Skip to content

Commit

Permalink
form, CreateView,
Browse files Browse the repository at this point in the history
inc.
+ auto tags
+ form simple validation
+ auto reverse redirect after form submitted & inserted to db
  • Loading branch information
SunnyBingoMe committed Sep 3, 2016
1 parent 556a8d3 commit 5b9e48a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 4 deletions.
6 changes: 4 additions & 2 deletions djangoSunnySite/blog2/models.py
@@ -1,12 +1,14 @@
from django.core.urlresolvers import reverse
from django.db import models



class Post(models.Model): # this will create a table "blog_post" in small letter format: "<app_name>_<class_name>"
title = models.CharField(max_length = 140) # optional max_length
body = models.TextField()
date = models.DateTimeField()

def get_absolute_url(self):
return reverse('blog2:detail', kwargs={'pk': self.pk}) # form inserted into db, then redirect to this url.

def __str__(self):
return self.title

25 changes: 25 additions & 0 deletions djangoSunnySite/blog2/templates/blog2/base.html
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>{% block title %}{% endblock %}</title>
<meta charset="utf-8" />
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type = "text/css"/>
<meta name="viewport" content = "width=device-width, initial-scale=1.0">

<style type="text/css">
html,
body {
height:100%
}
</style>
</head>

<body class="body">
{% block body %}

{% endblock %}
</body>

</html>
13 changes: 13 additions & 0 deletions djangoSunnySite/blog2/templates/blog2/form_template.html
@@ -0,0 +1,13 @@
{% for field in form %}
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<span class="text-danger small">
{{ field.errors }}
</span>
</div>
<label class="control-label col-sm-2">
{{ field.label_tag }}
</label>
<div class="col-sm-10">{{ field }}</div>
</div>
{% endfor %}
26 changes: 26 additions & 0 deletions djangoSunnySite/blog2/templates/blog2/post_form.html
@@ -0,0 +1,26 @@
{% extends "blog2/base.html" %}

{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="panel panel-default">
<div class="panel-body">

<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include "blog2/form_template.html"%}
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>

</div>
</div>
</div>
</div>
</div>

{% endblock %}
3 changes: 2 additions & 1 deletion djangoSunnySite/blog2/urls.py
Expand Up @@ -3,6 +3,7 @@


urlpatterns = [
url(r'^$', views.IndexView.as_view()),
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)$', views.DetailView.as_view(), name='detail'),
url(r'^post/create$', views.PostCreate.as_view(), name='post-create'),
]
6 changes: 6 additions & 0 deletions djangoSunnySite/blog2/views.py
@@ -1,4 +1,6 @@
from django.views import generic
from django.views.generic.edit import CreateView # more: UpdateView, DeleteView

from blog2.models import Post


Expand All @@ -13,3 +15,7 @@ def get_queryset(self):
class DetailView(generic.DetailView):
template_name = "blog2/one_post.html"
model = Post

class PostCreate(CreateView):
model = Post
fields = ['title', 'body', 'date']
Binary file modified djangoSunnySite/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion djangoSunnySite/djangoSunnySite/urls.py
Expand Up @@ -21,6 +21,6 @@
url(r'^webapp/', include('webapp.urls')),
url(r'^blog/', include('blog.urls')),
url(r'^api/', include('blog.urls', namespace='rest_framework')),
url(r'^blog2/', include('blog2.urls')),
url(r'^blog2/', include('blog2.urls', namespace='blog2')),
url(r'^', include('blog2.urls')),
]

0 comments on commit 5b9e48a

Please sign in to comment.