def post(self, request, *args, **kwargs): if "cancel" in request.POST: return redirect("blog:admin_index") context = self.get_context_data(**kwargs) entry = context.get("entry") new_entry = False if not entry: new_entry = True entry = Entry(blog=self.blog) form = EntryForm(request.POST, instance=entry) if form.is_valid(): entry = form.save() if request.is_ajax(): return JsonResponse(dict(status="success", message=unicode(form.success_message))) else: messages.success(self.request, form.success_message) if new_entry: return redirect("blog:admin_edit_entry", entry_id=entry.id) context["form"] = EntryForm(instance=entry) return self.render_to_response(context) else: if request.is_ajax(): errors = {} for field_name, errors_list in form.errors.items(): errors[field_name] = errors_list[0] return JsonResponse(dict(status="error", errors=errors, message=unicode(form.error_message))) else: messages.error(self.request, form.error_message) context["form"] = EntryForm(request.POST) return self.render_to_response(context)
def add(request): if request.method == 'POST': form = EntryForm(request.POST) # A form bound to the POST data if form.is_valid(): form.save(request.user) messages.success(request, 'Entry was successfully added!') return redirect('blog:index') else: errors = [] for field in form: for error in field.errors: errors.append(field.label + " " + error) for error in form.non_field_errors(): errors.append(field.label + " " + error) print(errors) else: form = EntryForm() context = {'form': form} return render(request, 'blog/entry.html', context)
def newpost(request): if request.POST: entryform = EntryForm(request.POST) if entryform.is_valid(): entryform.save(request) messages.add_message(request, messages.WARNING, 'Your post is waiting to approve by admin.') return render_to_response('home.html', RequestContext(request)) else: entryform = EntryForm() return render_to_response('newpost.html', {'form' : entryform}, RequestContext(request))
def edit(request, pk): entry = get_object_or_404(Entry, pk=pk) if request.method == "POST": form = EntryForm(request.POST, instance=entry) if form.is_valid(): entry = form.save() return redirect("blog.views.detail", entry.pk) else: form = EntryForm(instance=entry) return render(request, "blog/form.html", {"form": form})
def BlogPage(request): if request.method == 'POST': form = EntryForm(request.POST, request.FILES) if form.is_valid(): newdoc = models.Entry(image1 = request.FILES['image1']) newdoc.save() return HttpResponseRedirect('/blog') else: form = EntryForm() entries = models.Entry.objects.all() return render_to_response('blog.html', {'entries': entries, 'form': form}, context_instance = RequestContext(request))
def new(request): if request.method == "POST": form = EntryForm(request.POST) if form.is_valid(): entry = form.save(commit=False) entry.author = request.user entry.save() return redirect("blog.views.detail", entry.pk) else: form = EntryForm() return render(request, "blog/form.html", {"form": form})
def newpost(request): if request.POST: entryform = EntryForm(request.POST) if entryform.is_valid(): entryform.save(request) messages.add_message(request, messages.WARNING, 'Your post is waiting to approve by admin.') return render_to_response('home.html', RequestContext(request)) else: entryform = EntryForm() return render_to_response('newpost.html', {'form': entryform}, RequestContext(request))
def entry_add(request): if request.method == 'POST': form = EntryForm(request.POST, request.FILES) if form.is_valid(): entry = form.save(request.user) if entry.id > 0: return HttpResponseRedirect(entry.get_absolute_url()) else: form_to_template = form else: form_to_template = EntryForm() return render_to_response('entry_modify.html',{'form': form_to_template, 'edit': False}, context_instance = RequestContext(request))
def edit(request, entry_id): entry = get_object_or_404(Entry, id=entry_id) if request.method == 'POST': form = EntryForm(request.POST, instance=entry) if form.is_valid(): form.save() messages.success(request, 'Entry was successfully edited!') return redirect('index') else: form = EntryForm(instance=entry) context = {'form': form} return render(request, 'blog/entry.html', context)
def entry_edit(request, id): entry = get_object_or_404(Entry, pk=id) if entry.user != request.user: return HttpResponseRedirect(entry.get_absolute_url()) if request.method == 'POST': form = EntryForm(request.POST, request.FILES, instance=entry) if form.is_valid(): entry = form.save(request.user) return HttpResponseRedirect(entry.get_absolute_url()) else: form_to_template = form else: form_to_template = EntryForm(instance=entry, initial={'tags': ','.join([tag.name for tag in entry.tags.all()])}) return render_to_response('entry_modify.html',{'form': form_to_template, 'edit': True}, context_instance = RequestContext(request))
def edit(request,entry_id): e = get_object_or_404(Entry, id=entry_id) if request.user.id == e.author.id: if request.POST: entryform = EntryForm(request.POST) if entryform.is_valid(): entryform.save_as(request,e) messages.add_message(request, messages.WARNING, 'Your post is waiting for approvement.') return HttpResponseRedirect('/') else: entryform = EntryForm({'title' : e.title, 'content' : e.content}) return render_to_response('edit.html', {'entry_id' : entry_id, 'form' : entryform}, RequestContext(request)) else: messages.add_message(request, messages.ERROR, 'You can not edit this post, because you are not the author of it.') return HttpResponseRedirect('/')
def create_entry(request): form = EntryForm() if (request.method == 'POST'): form = EntryForm(data=request.POST) if (form.is_valid()): entry = form.save(False) entry.user = request.user entry.save() return redirect(reverse('blog.views.blog_post', args=[entry.id])) return render_to_response('blog/create_entry.html', {'form': form}, context_instance=RequestContext(request))
def post(request, post_id=None): """ post a new post """ post = get_object_or_404(Post, id=post_id) if post_id else None if request.method == 'POST': form = EntryForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) post.user = request.user try: post.content_html = format_content(post.content_format, post.content) post.save() return redirect(post.get_absolute_url()) except Exception, e: messages.error(request, e.message)
def edit(request,slug=None): entry = None if slug: try: entry = Entry.objects.get(slug=slug) except Entry.DoesNotExist: pass c = RequestContext(request) c.update(csrf(request)) if request.method == 'POST': form = EntryForm(request.POST,request.FILES) if form.is_valid(): # If editting if entry: entry.content = form.cleaned_data['content'] entry.brief = form.cleaned_data['brief'] entry.public = form.cleaned_data['public'] entry.title = form.cleaned_data['title'] entry.created = form.cleaned_data['created'] if request.FILES.__contains__('image') and request.FILES['image']: try: entry.image.delete() except: pass entry.image.save(entry.slug+os.path.splitext( request.FILES['image'].name )[1],ContentFile(request.FILES['image'].read())) #handle_uploaded_file(request.FILES['image'],str(entry.image.file)) # If NOT editting else: entry = form.save(commit=False) entry.author = request.user if request.FILES.__contains__('image') and request.FILES['image']: entry.image.save(entry.slug+os.path.splitext( request.FILES['image'].name )[1],ContentFile(request.FILES['image'].read())) entry.save() return HttpResponseRedirect(ROOT_URL) else: return HttpResponse(str(form.errors)) form = EntryForm(instance=entry) return render_to_response('edit.html', dict(slug=slug,form=form), context_instance=c)
def post(self, request): form = EntryForm(request.POST) if form.is_valid(): entry = form.update() url = reverse_lazy('blog:entry-detail', kwargs={ 'pk': entry.id }) return HttpResponseRedirect(url) context = { 'error_messages': form.error_messages } return TemplateView(request, self.template_url, context)
def edit_entry(request, entry_id): '''编辑既有条目''' entry = Entry.objects.get(id=entry_id) topic = entry.topic if topic.owner != request.user: raise Http404 if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(data=request.POST, instance=entry) if form.is_valid(): form.save() return redirect(reverse('blog:topic', args=[topic.id])) context = {'form': form, 'title': '编辑条目页面', 'topic': topic, 'entry': entry} return render(request, 'blog/edit_entry.html', context)
def new_entry(request, topic_id): '''添加新条目''' topic = Topic.objects.get(id=topic_id) if topic.owner != request.user: raise Http404 if request.method != 'POST': form = EntryForm() else: form = EntryForm(request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return redirect(reverse('blog:topic', args=[topic_id])) context = {'form': form, 'title': '条目创建页面', 'topic': topic} return render(request, 'blog/new_entry.html', context)
def new_entry(request, topic_id): topic = Topic.objects.get(id=topic_id) if topic.author != request.user: raise Http404 if request.method != 'POST': # 未提交数据:创建一个新表单 form = EntryForm() else: # POST提交的数据,对数据进行处理 form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return HttpResponseRedirect(reverse('blog:topic', args=[topic_id])) context = {'topic': topic, 'form': form} return render(request, 'blog/new_entry.html', context)
def edit_entry(request, entry_id): """编辑条目""" entry = Entry.objects.get(id=entry_id) topic = entry.topic if topic.author != request.user: raise Http404 if request.method != 'POST': # 未提交数据:创建一个新表单 form = EntryForm(instance=entry) else: # POST提交的数据,对数据进行处理 form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('blog:topic', args=[topic.id])) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'blog/edit_entry.html', context)
def add_entry(request): # Process the submitted form if POST, displaying errors if fields required if request.method == 'POST': form = EntryForm(request.POST) if form.is_valid(): entry = form.save(commit=False) entry.author = request.user entry.save() return HttpResponseRedirect('/') # Display blank entry form normally else: form = EntryForm() context = { 'form': form } return render(request, 'blog/add.html', context)
def add_entry(request): host = 'http://%s' % request.get_host() if request.method == 'POST': form =EntryForm(request.POST) if form.is_valid(): entry = form.save(commit=False) entry.subscription = None entry.owner = request.user entry.author = request.user.username entry.subscribers_usernames = [request.user.username] # let the author_url blank so it won't be hardcoded in the store and # in the template, if author_link is empty, generate a link to his # entries entry.author_url = '' entry.save() # invalidate cache clear_entries_cache(request.user.username) # inform the hub post_publish.send(sender=Entry, username=entry.author, host=host) if request.is_ajax(): e = dict( author=request.user.username, author_url = host+'/'+request.user.username, body = entry.body, link = entry.link, linktext = entry.link, key = str(entry.key()), permalink = '/show/'+str(entry.key()), editlink = '/edit/'+str(entry.key()), deletelink = '/delete/'+str(entry.key()) ) return json_response(e) else: return HttpResponseRedirect(reverse('user-entries', kwargs={'username': request.user.username})) else: form =EntryForm() return render_to_response('entry_form.html', { 'form': form })
def test_valid_data(self): entry_form = EntryForm({ 'title': self.entry.title, 'body': 'entry body', 'author': self.user.id }) self.assertTrue(entry_form.is_valid()) comment_form = CommentForm({ 'name': 'Test form user', 'email': '*****@*****.**', 'body': 'body content', 'entry': self.entry.id }, instance=self.comment) self.assertTrue(comment_form.is_valid()) comment = comment_form.save() self.assertEqual(comment.name, 'Test form user') self.assertEqual(comment.email, '*****@*****.**') self.assertEqual(comment.body, 'body content') self.assertEqual(comment.entry.id, self.entry.id)
def edit(request, entry_id): e = get_object_or_404(Entry, id=entry_id) if request.user.id == e.author.id: if request.POST: entryform = EntryForm(request.POST) if entryform.is_valid(): entryform.save_as(request, e) messages.add_message(request, messages.WARNING, 'Your post is waiting for approvement.') return HttpResponseRedirect('/') else: entryform = EntryForm({'title': e.title, 'content': e.content}) return render_to_response('edit.html', { 'entry_id': entry_id, 'form': entryform }, RequestContext(request)) else: messages.add_message( request, messages.ERROR, 'You can not edit this post, because you are not the author of it.' ) return HttpResponseRedirect('/')
def edit_entry(request, entry_id): entry = get_object_or_404(Entry, pk=entry_id) if request.method == 'POST': form = EntryForm(request.POST, instance=entry) if form.is_valid(): # Change the modified to now before saving entry = form.save(commit=False) entry.modified = datetime.datetime.now() entry.save() return HttpResponseRedirect('/') # The instance fills the form with values from requested entry else: form = EntryForm(instance=entry) context = { 'form': form, 'entry': entry, } return render(request, 'blog/edit.html', context)
def write(request): me = Member.objects.get(user=request.user) dbmessage = "" if request.method == "POST": form = EntryForm(request.POST) if form.is_valid(): post_text = form.cleaned_data["text"] entry = form.save(commit=False) entry.member = me entry.save() message = "Journal entry saved." return render_to_response( "public_site/home.html", {"message": message, "dbmessage": dbmessage}, context_instance=RequestContext(request), ) else: form = EntryForm() return render_to_response("blog/write.html", {"form": form}, context_instance=RequestContext(request))
def post(self, request): form = EntryForm(request.POST) if form.is_valid(): Entry.objects.create(**form.cleaned_data) return redirect("/entry_list/")