def testdb_init(): # db clear t1 = Tag(title="emacs") t2 = Tag(title="python") t1.put() t2.put() c1 = Category(title='program') c2 = Category(title='edit') c1.put() c2.put() b1 = Blog(title='first blog') b1.context = "this is my first blog, hello world" b1.put() b2 = Blog(title="second blog") b2.context = "this is my second blog, hello python" b2.tags = [t1.key, t2.key] b2.put() b3 = Blog(title="third blog") b3.context = "this is my third blog, hello python" b3.tags = [t1.key,] b3.category = c2.key b3.put() b4 = Blog(title="fourth blog") b4.context = "this is my fourth blog, hello python" b4.tags = [t2.key,] b4.category = c1.key b4.put()
def create(request): if not admin(): return HttpResponseRedirect(users.create_login_url("/blogs")) form = BlogForm(request.POST) form.fields["category"].choices = [("", "请选择")] + [(category.key(), category.name) for category in Category.all()] if form.is_valid(): blog = Blog() blog.title = form.cleaned_data["title"] blog.content = form.cleaned_data["content"] if form.cleaned_data["category"]: blog.category = Category.get(form.cleaned_data["category"]) elif form.cleaned_data["user_tag"]: blog.category = Category.get_or_insert(form.cleaned_data["user_tag"], name=form.cleaned_data["user_tag"]) blog.create() return HttpResponseRedirect("/blogs") else: return new(request)
def create(request): if not admin(): return HttpResponseRedirect(users.create_login_url('/blogs')) form = BlogForm(request.POST) form.fields['category'].choices = [('', '请选择')] + [(category.key(), category.name) for category in Category.all()] if form.is_valid(): blog = Blog() blog.title = form.cleaned_data['title'] blog.content = form.cleaned_data['content'] if form.cleaned_data['category']: blog.category = Category.get(form.cleaned_data['category']) elif form.cleaned_data['user_tag']: blog.category = Category.get_or_insert(form.cleaned_data['user_tag'], name=form.cleaned_data['user_tag']) blog.create() return HttpResponseRedirect('/blogs') else: return new(request)