Commits

Hudarsono  committed dca1bbe

got validation post working

  • Participants
  • Parent commits caf905b

Comments (0)

Files changed (6)

File posts/models.py

 import datetime
 
 class Post(db.Model):
-    title = db.StringProperty()
-    body = db.TextProperty()
-    category = db.CategoryProperty()
-    tags = db.StringListProperty()
+    title = db.StringProperty(required=True)
+    body = db.TextProperty(required=True)
+    category = db.CategoryProperty(required=True)
+    tags = db.StringListProperty(required=True)
     pub_date = db.DateTimeProperty(auto_now_add=True)
     author = db.UserProperty(auto_current_user_add=True)
 

File posts/models.pyc

Binary file modified.

File posts/views.py

 											  'tags': cat_tag['tag_list']})
 
 def newPost(request):
+	postForm = None
 	if request.method == 'POST':
 		newPost = postform.NewPostForm(request.POST)
-		newPost.title = request.POST.get('title')
-		newPost.body = request.POST.get('body')
-		newPost.category = request.POST.get('category')
-		newPost.tags = request.POST.get('tags').split()
-		newPost.save()
-		return HttpResponseRedirect('/posts/')
+		if newPost.is_valid():
+			newPost.save()
+			return HttpResponseRedirect('/posts/')
+		else:
+			postForm = postform.NewPostForm(request.POST)
 
-	if request.method == 'GET':
+
+	if postForm is None:
 		postForm = postform.NewPostForm()
-		return render_to_response('admin_newpost.html', {
-														 'postForm':postForm})
+	return render_to_response('admin_newpost.html', {
+													'postForm':postForm})
 
 def editPost(request, year, month, day, key):
 	if request.method == 'POST':

File posts/views.pyc

Binary file modified.

File resources/css/admin.css

 	-moz-border-radius-topright:8px;
 	-webkit-border-top-right-radius:6px;
 	border-top-right-radius:6px;
+}
+
+form p input{
+	width:350;
+}
+
+form ul.errorlist{
+	color:red;
+	list-style:none;
+	display:inline;
 }

File templates/admin_newpost.html

 <div style="width:400px">
 	<form method="POST" action="{% if action %}{{ action }}{% else %}/post/new/{% endif %}">
 		<p>
-		<label for="id_title">Title : </label><br />
+		<label for="id_title">Title : </label>{{ postForm.title.errors }}<br />
 		{{ postForm.title }}
 		</p>
 		<p>
 		</p>
 		<p>
 		<label for="id_tags">Tags (separate by space): </label><br />
-		{{ postForm.tags }}
+		<input type="text" name="tags" id="id_tags"/>
 		</p>
 	<input type="submit" value="Publish"/>
 	</form>