Django Form Follow Up

作者 Scott Hou 日期 2016-08-10
Django Form Follow Up

This blog is a follow up of the last post Working with Django Form.

The problem has been solved shortly after the first post. I found out that the real view which renders the user.html is another view called view.index. So after I altered the index view everything worked just fine.

Moreover, I managed to customize the default form. The default form is something like this:

1
2
3
4
5
6
7
8
9
10
<tr><th><label for="id_user">User:</label></th><td><select id="id_user" name="us
er">
<option value="" selected="selected">---------</option>
<option value="3">test_user</option>
<option value="1">hsj</option>
<option value="2">houshijun</option>
</select></td></tr>
<tr><th><label for="id_text">Anything on your mind?</label></th><td><input id="i
d_text" maxlength="160" name="text" type="text" value="Anything on your mind?" /
></td></tr>

As you can see, there`s a modelchoice field to select the user which is the ForeignKey field and a text input field for field text. But obviously the typical tweet interface is comprised solely of an input field because the user who has already logged in is the author of the tweet. Given the situation, in the view I changed how the POST data is passed to the form:

1
2
3
4
5
6
7
8
if request.method == 'POST':
#create a form instance from post data.the authenticated user is passed only the POST`s text field is needed
form = TweetForm(
{
'user': request.user.pk,
'text': request.POST['text']
}
)

instead of passing the whole package of POST data to the form, only the text field is passed and the user field is filled with the key of request.user so that the author of the tweet will always be the user who has already logged in.

In addition, I overrode the default error message and initialized the text field:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TweetForm(ModelForm):
class Meta:
model = Tweet
fields = '__all__'
#override the text label
labels = {
'text': _('Anything on your mind?')
}
error_messages = {
'text': {
'required': 'Why not wait until you actually have something to tweet.'
}
}
def __init__(self, *args, **kwargs):
super(TweetForm, self).__init__(*args, **kwargs)
self.fields["text"].initial = "Anything on your mind?"

Hooray! Now I`ve got something close to a timeline and the user can post their own tweets. Still, the timeline only has the user`s own tweets because he hasn`t followed anybody and that`s one of the many things I need to work on.

Apart from the basic function of a typical socialnetwork application, the layout of the website is currently a mess. I basically hardcoded every detail of the site using .css which is a totally stupid thing to do. Using Bootstrap to decorate the site is another thing goes into my to-do-list.

To summarize, I, again, make two lists of what I got and what to do:

Achievements

  • Basic authentication function
  • A timeline which stores all the tweets from the user himself.
  • Allowing user to post his own tweets.

Way to go

  • Bootstraping my website.
  • Follow function.