36π
S. Lottβs answer tells you how to initialize the form with some data in your view. To render your form in a template, see the following section of the django docs which contain a number of examples:
Although the examples show the rendering working from a python interpreter, itβs the same thing when performed in a template.
For example, instead of print f
, your template would simply contain: {{ f }}
assuming you pass your form through the context as f
. Similarly, f.as_p()
is written in the template as {{ f.as_p }}
. This is described in the django template docs under the Variables section.
Update (responding to the comments)
Not exactly, the template notation is only for template. Your form and associated data are initialized in the view.
So, using your example, your view would contain something like:
def view(request):
game = Game.objects.get(id=1) # just an example
data = {'id': game.id, 'position': game.position}
form = UserQueueForm(initial=data)
return render_to_response('my_template.html', {'form': form})
Then your template would have something like:
{{ form }}
Or if you wanted to customize the HTML yourself:
{{ form.title }} <br />
{{ form.genre }} <br />
and so on.
I recommend trying it and experimenting a little. Then ask a question if you encounter a problem.
14π
http://docs.djangoproject.com/en/1.2/ref/forms/api/#ref-forms-api-bound-unbound
To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor:
>>> data = {'subject': 'hello',
... 'message': 'Hi there',
... 'sender': 'foo@example.com',
... 'cc_myself': True}
>>> f = ContactForm(data)
- [Django]-Why don't my south migrations work?
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-Debugging Apache/Django/WSGI Bad Request (400) Error
11π
Do you could try the follow:
from django.forms.models import model_to_dict
def view(request):
game = Game.objects.get(id=1)
form = UserQueueForm(initial=model_to_dict(game))
return render_to_response('my_template.html', {'form': form})
works fine for me on Django 1.8
- [Django]-Query for top x elements in Django
- [Django]-Why don't my south migrations work?
- [Django]-Why does my Django admin site not have styles / CSS loading?
6π
When using CBVs, you can override the get_initial()
FormMixin to populate initial data into the form.
Example:
class MyView(FormView):
def get_initial(self):
initial = super(MyView, self).get_initial()
initial['start_date'] = datetime.date.today()
return initial
See the docs.
- [Django]-Django manage.py runserver invalid syntax
- [Django]-How to add data into ManyToMany field?
- [Django]-How to change empty_label for modelForm choice field?
4π
just change
data = {'title':'{{game.title}}','genre':'{{game.genre}}'}
form(data)
to
data = {'title':'{{game.title}}','genre':'{{game.genre}}'}
form(initial=data)
- [Django]-Why don't my Django unittests know that MessageMiddleware is installed?
- [Django]-Django Rest Framework: Disable field update after object is created
- [Django]-How can I build multiple submit buttons django form?
2π
You can pass the instance directly if you set the model attribute on the form class
class GameForm(forms.Form):
class Meta:
model = Game
In your view:
def view(request):
game = Game.objects.get(id=1)
form = GameForm(instance=game)
return render(request, 'template.html', {'form': form})
Works for me on django 2.1
- [Django]-Tailwindcss: fixed/sticky footer on the bottom
- [Django]-Django: how can I tell if the post_save signal triggers on a new object?
- [Django]-How to do SELECT MAX in Django?
0π
I think this will work too, but not sure.
def view(request):
game = Game.objects.get(id=1) # just an example
form = UserQueueForm(instance=game)
return render_to_response('my_template.html', {'form': form})
And in the view you can do like form.field to show form with intial data.
- [Django]-How to recreate a deleted table with Django Migrations?
- [Django]-Django Error u"'polls" is not a registered namespace
- [Django]-Django, filter by specified month and year in date range