Skip to content
Permalink
Browse files

form, CreateView,

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 5b9e48a89d7cbfb5121db606b870561205b9cd0f
@@ -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

@@ -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>
@@ -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 %}
@@ -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,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'),
]
@@ -1,4 +1,6 @@
from django.views import generic
from django.views.generic.edit import CreateView # more: UpdateView, DeleteView

from blog2.models import Post


@@ -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']
BIN +12 KB (130%) djangoSunnySite/db.sqlite3
Binary file not shown.
@@ -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.
You can’t perform that action at this time.