summaryrefslogtreecommitdiff
path: root/tutorial_8_django_forms/slides.md
diff options
context:
space:
mode:
authorankitjavalkar2017-07-03 16:58:21 +0530
committerGitHub2017-07-03 16:58:21 +0530
commit02bb99f9b7cdaba59aa55a7da977ae3a87875f3f (patch)
tree3020db9f397570824ab5963a04d4689e8fd4ad72 /tutorial_8_django_forms/slides.md
parent6a33a2a2dca1f0a5b743184a8af3536dc22e7a94 (diff)
downloadlearn_django-02bb99f9b7cdaba59aa55a7da977ae3a87875f3f.tar.gz
learn_django-02bb99f9b7cdaba59aa55a7da977ae3a87875f3f.tar.bz2
learn_django-02bb99f9b7cdaba59aa55a7da977ae3a87875f3f.zip
Add views and templates to tutorial
Diffstat (limited to 'tutorial_8_django_forms/slides.md')
-rw-r--r--tutorial_8_django_forms/slides.md97
1 files changed, 85 insertions, 12 deletions
diff --git a/tutorial_8_django_forms/slides.md b/tutorial_8_django_forms/slides.md
index 1caf256..98ae875 100644
--- a/tutorial_8_django_forms/slides.md
+++ b/tutorial_8_django_forms/slides.md
@@ -56,31 +56,104 @@ Slide 7
- Create a forms.py in the ```blog``` and add the code
- class BlogForm(ModelForm):
- class Meta:
- model = Blog
- fields = ['name', 'created_on']
+ class BlogForm(ModelForm):
+ class Meta:
+ model = Blog
+ fields = ['name', 'created_on']
- class ArticleForm(ModelForm):
- class Meta:
- model = Article
- fields = ['created_on', 'title', 'body', 'draft']
+ class ArticleForm(ModelForm):
+ class Meta:
+ model = Article
+ fields = ['created_on', 'title', 'body', 'draft']
Slide 8
----------------
**Adding a new view to handle the form**
-Slide 7
------------------
-
-**Creating an HTML Form**
+- Let's add a new view to add a new blog or edit an existing blog
+- To do this add the following code to the ```views.py``` in the ```blog```directory
+
+ from django.http import HttpResponse, HttpResponseNotFound
+ from .forms import BlogForm, ArticleForm
+
+ def edit_blog(request, blog_id):
+ """
+ This view adds a new or edits an existing blog
+ """
+ if blog_id:
+ blog = Blog.objects.get(id=blog_id)
+ else:
+ blog = Blog()
+ if request.method == 'GET':
+           blog_form = BlogForm(instance=blog)
+ context = {'form': blog_form, 'blog': blog}
+ return render(request, 'blog/blog_form.html', context)
+ elif request.method == 'POST':
+ form = BlogForm(request.POST, instance=blog)
+ if form.is_valid():
+ form.save()
+ return redirect('index')
+ else:
+ return redirect('edit_blog')
+
+ def edit_article(request, blog_id, article_id):
+ """
+ This view adds a new or edits an existing article
+ """
+ blog = Blog.objects.get(id=blog_id)
+ if article_id:
+ article = Article.objects.get(id=article_id)
+ else:
+ article = Article(blog=blog)
+ if request.method == 'GET':
+           article_form = ArticleForm(instance=article)
+ context = {'form': article_form, 'article': article}
+ return render(request, 'blog/article_form.html', context)
+ elif request.method == 'POST':
+ if article == None:
+ form = ArticleForm(request.POST, instance=article)
+ else:
+ form = ArticleForm(instance=article)
+ if form.is_valid():
+ form.save()
+ return redirect('index')
+ else:
+ return redirect('edit_article')
Slide 8
-----------------
**Creating a Template for the view**
+- Create a template ```edit_blog.html``` at ```/blog/templates/blog/edit_blog.html``` to look like below
+
+ <html>
+ <body>
+ {% if form %}
+ <form action="/blogs/edit_blogs/{{ blog.id }}" method="post">
+ {% csrf_token %}
+ {{ form }}
+ <input type="submit" value="Submit" />
+ </form>
+ {% endif %}
+ </body>
+ </html>
+
+- Create a template ```edit_article.html``` at ```/blog/templates/blog/edit_article.html``` to look like below
+
+ <html>
+ <body>
+ {% if form %}
+ <form action="/blogs/edit_blogs/{{ blog.id }}/{{ article.id}}" method="post">
+ {% csrf_token %}
+ {{ form }}
+ <input type="submit" value="Submit" />
+ </form>
+ {% endif %}
+ </body>
+ </html>
+
Slide 9
-----------------