Here is an example having: form with three fields i.e
from django import forms from models import Article class ArticleForm(forms.ModelForm): class Meta: model = Article fields = ('title','body','thumbnail') view
from django.shortcuts import render_to_response from uploadfiles.models import Article from django.http import HttpResponse, HttpResponseRedirect from forms import ArticleForm from django.core.context_processors import csrf def create (request): if request.POST: form = ArticleForm(request.POST, request.FILES) if form.is_valid(): return HttpResponseRedirect('/all') else: form = ArticleForm() args= {} args.update(csrf(request)) args['form'] = form return render_to_response('create_article.html', args) models
from django.db import models from time import time def get_upload_file_name(request): return "uploaded_files/%s_%s" %(str(time()).replace('.','-')) class Article(models.Model): title = models.CharField(max_length=200) body = models.TextField() thumbnail = models.FileField(upload_to = get_upload_file_name) def __unicode__(self): return self.title html page
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <form action="/create" method="post" enctype="multipart/form-data">{% csrf_token %} {{form.as_ul}} <input type="submit" name="submit" value="create"/> </form> </body> </html> MY QUESTIONS ARE:
1)What is Meta class, why we use this?
2)What this line means args.update(csrf(request))?
3)As in forms page redirects to /create .. as this can be any page! so how to save posted data now. as this returns the submitted data to html page.
My question can be so basic or simple but these are the things that are not clear to me and for that reason i am posting this here! and it can be duplicate so if you don't like it please don't mark negatively.:)