4

I am building my own projects based on the instructions from 'Django by Example' book.

However I got stuck on how does django initialize input form fields using URL parameters?

#models.py
class Image(models.Model):  
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    url = models.URLField()
    (...)

#views.py
def image_create(request):
    if request.method == 'POST':
        form = ImageCreateForm(data=request.POST)
        (...)
    else:
        form = ImageCreateForm(data=request.GET)
    return render(request,
              'create.html',
              {'form': form})
#forms.py
class ImageCreateForm(forms.ModelForm):
    class Meta:
        model = Image
        fields = ('title', 'url', 'description')
        widgets = {
            'url': forms.HiddenInput,
        }
#create.html
{% extends "base.html" %}
{% block content %}
<h1>Bookmark an image</h1>
<img src="{{ request.GET.url }}" class="image-preview">
<form action="." method="post">
    {{ form.as_p }}
    {% csrf_token %}
    <input type="submit" value="Bookmark it!">
</form>
{% endblock content %}

I could not find any specific line of code which would explicitly tell to get request.GET parameters and assign each of them to corresponding input field. I assume that all of this has to do with the form initialization on line: form = ImageCreateForm(data=request.GET) in views.py

Questions:

  1. Can someone please explain me how does django use request.GET parameters to initialize input fields values(assigns them URL parameters' values)? Does it simply match, for every request.GET key, the corresponding input field 'name' attribute, and then assigns to that particular input field the value corresponding to the key in request.GET?
  2. Also can someone confirm if there is a relationship between the model/form(which one?) field names to the URL parameters?
  3. What is the difference between these two(as both of them seems to work the same way, except the latter returns list in the input field values):

    form = ImageCreateForm(data=request.GET)

    form = ImageCreateForm(initial=request.GET)

2
  • There are at least 3 questions here, ideally it should be one. (Also better to ask them separately to increase your chances of getting an answer.) Commented Jul 26, 2016 at 3:56
  • @TinyInstance Thanks for the pointers. I've just thought that if all the questions are related to the same problem would be easier to ask them in the same post. Commented Jul 26, 2016 at 11:54

1 Answer 1

1

1) Yes - request.GET is simply a fancy dictionary (technically a QueryDict), and the form "data" argument expects a dict with keys corresponding to the fields in the form. You could technically pass the form any dictionary if it had the right keys representing the fields.

2) Generally speaking, there is no relation between model/form fields and url parameters. However, if you are using certain Class Based Views (such as a DetailView) if you set a slug or id in your url, it will pass the value along to the view and map it to the objects slug or id. The get_object() method in the link below has an example.

https://ccbv.co.uk/projects/Django/1.9/django.views.generic.detail/DetailView/

3) The data attribute to a form is what is submitted (usually POSTed) to the form whereas the initial value is what is passed to the form upon first page load. For example, if you were editing an object, typically the existing object data would populate the initial value. Then, if you POSTed edits to the form, it would be passed in as data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.