Working with Django Form

作者 Scott Hou 日期 2016-08-01
Working with Django Form

Still in the middle of buliding the myinstagram django project and it`s not going so well. Though I have successfully incorporated getstream API and use the activity_enricher to enrich every tweet, I`m having trouble creating the timeline for the user.

Right now I can:

  1. create tweet from python shell using Tweet.objects.create() method to create tweets with specific user and tweet text.
  2. login and see all the tweets the user has ever tweeted.

What I`m going to do is:

  1. incorporate UI feature where user could create tweet on the webpage, basically just a input box.
  2. display other users so that they can be followed.

Django Form

The problem I`m having now is dealing with Django Form. The Django Form is basically a <form> tag in html but more than that. It`s the interface where user could interact with the django database, to create new instances of django models or alter the fields of existing database instances, for example. So this is a little background on the Form.

The mechanism of how Django Form works is like this:

First of all, there need to be a forms.py file in the project and a Form class should be defined. There are two ways to create a Form class:

  1. Subclassing the django.forms.Form and specify every field that you expect to use.
  2. Creating forms based on existing models. To do this I can just from django.forms import ModelForm and import the model I need then appoint my model to the model attribute of the ModelForm.

Secondly, I need to tell the view to render the form and process the data which will be posted by users to create their own tweets. This process involves checking the method of the HTTP request: all the tweet should be a HTTP POST request. Apart from this, the data which is passed through the POST need to be validated before it can be processed by django.

And finally, in the html template a snippet of codes is required to actually render the form. It`s simply like this:

1
2
3
4
5
<form action="" method="post">
{% csrf_token %}
{% form %}
<input type="submit" value="Tweet" />
</form>

And that`s it!

But in reality, it doesn`t work. And I`ve been trying to figure out why but so far I didn`t have any luck. I kind of figured out what went wrong but I can`t explain it nor can I solve the problem.

The thing is, I can actually render the form using python shell and it`s a bunch of html form tags and codes which are exactly what I need. However, the in the html template is not working. So this led me to check whether the view has correctly render the context. The context looks like this:

1
2
3
4
5
6
7
context = {
'activities': activities,
'user': user,
'login_user': request.user,
'form': form
}
return render(request, 'look/user.html', context)

all the other key / value pairs work just fine but the form variable is not available in the .html. I did something like this:

1
2
3
{% if form %} form is true
{% else %} form is false
{% endif %}

the result is form is false which indicates although I passed the TweetForm instance to the context, somehow it`s not available in the html and I can`t explain what is going on because it should have worked.
I already posted a question on stackoverflow: Cant`t render forms. Hopefully someone could help me out on this one.