Skip to content

Instantly share code, notes, and snippets.

@Jbot29
Last active February 8, 2016 15:45
Show Gist options
  • Save Jbot29/7aca5eda7961411f6a06 to your computer and use it in GitHub Desktop.
Save Jbot29/7aca5eda7961411f6a06 to your computer and use it in GitHub Desktop.
Blog Create Form:
We are going to create a html form that accepts data from the user and creates a new blog entry/model.
When a user enters a url in Chrome like /blog, Chrome sends a request to the server for that url with the HTTP verb or action
of GET.
The http request will look like this
GET /blog ....
There are several HTTP verb types. GET,POST,PUT,DELETE
In order to build a form we are going to use the POST verb/action.
HTML has a form tag <form></form>
This form tag allows to recieve data from a user.
The form can take several attributes like action or method.
<form action='/blog/new/' method='post'>
Action is to which url to send the request to.
Method is the http verb to request.
There is a special tag that when used in form
<input type = "submit" value="Create">
When this line is added, a button will show in the form. When this button is clicked, the browser will submit the form.
What that means is it will send an http request to the url in action with the method/http verb
EX:
POST /blog/new ...
csrfmiddlewaretoken:zo6cGKMbj9pFOHxndNdEmK9pmSfIXeNc
title:sometitle
content:somecontent
The browser send a request, packages up the input tag like
<input name = 'title'>
The browser will use the name attribute, take the data that the user entered in that field in the form and
build an http request with that in as a key value pair
<value of the name attribute>:<data entered by user>
In Django, every view handler, or function in views.py takes a request object.
def blognew(request):
We can use this request object to see the HTTP method like GET or POST as well as those parameters that came from the form.
A typical pattern in Django is to have just one view handler for a url. In this case /blog/new
If the user types it in or goes to it through a link, it will be a GET request. So we want to show the form to the user.
If the user hits submit on the form, then the HTTP Request we get is a POST. So using inspecting the request.method
we can either return the form, or save the form data to the database.
1)add a new route/url to url patterns in blog/urls.py
url(r'^blog/new/$', 'blog.views.blognew', name='blognew'),
2)Edit views.py and add a new view handler/function for that new url
#add the line in the top part of views.py
from django.shortcuts import redirect
def blognew(request):
if request.method == 'GET' or request.method != 'POST':
return render(request,"blog/newblog.html",{})
title = request.POST.get('title')
content = request.POST.get('content')
return redirect('/blog')
3) Create newblog.html in blog/blog/templates/blog/
<form action='/blog/new/' method='post'>
{% csrf_token %}
<p>Title:</p>
<input name = 'title'>
<p>Content:</p>
<input name = 'content'>
<br></br>
<input type = "submit" value="Create">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment