def new_entry(request, topic_id): topic = Topic.objects.get(id=topic_id) if request.method != 'POST': form = EntryForm else: 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('learning_logs:topic', args=[topic_id])) context = {'topic': topic, 'form': form} return render(request, 'learning_logs/new_entry.html', context=context)
def edit_entry(request, topic_pk, entry_pk): entry = Entry.objects.get(pk=entry_pk) topic = entry.topic if topic.owner != request.user: raise Http404 if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(request.POST, instance=entry) if form.is_valid(): #entry = form.save(commit=False) #entry.date_added = timezone.now() entry.save() return redirect('../') return render(request, 'learning_logs/edit_entry.html', {'form': form, 'topic_pk': topic_pk, 'entry_pk': entry_pk})
def new_entry(request, topic_id): """Add new entry for a particular topic""" topic = Topic.objects.get(id=topic_id) check_topic_owner(request, topic) if request.method != 'POST': #No data submitted, create a blank form form = EntryForm else: #Post data submitted, process data form = EntryForm(request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return HttpResponseRedirect( reverse('learning_logs:topic', args=[topic_id])) return render(request, 'new_entry.txt', {'topic': topic, 'form': form})
def edit_entry(request,entry_id): entry=Entry.objects.get(id=entry_id) topic=entry.topic if request.method!='POST': form=EntryForm(instance=entry)#创建一根EntryForm表单实例,并使用instance是他可以把当前的文本填充 else: #用户将可以看到并编辑 form=EntryForm(instance=entry,data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reversed('learning_logs:topics',args=[topic.id])) #return render(request,'learning_logs/topic.html') context={'entry':entry,'topic':topic,'form':form} return render(request,'learning_logs/edit_entry.html',context)
def endit_entry(request, entry_id): entry = Entry.objects.get(id=entry_id) topic = entry.topic if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('learning_logs:topic', args=[topic.id])) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'learning_logs/edit_entry.html', context)
def edit_entry(request, entry_id): """Edits an existing entry.""" entry = get_object_or_404(Entry, id=entry_id) topic = entry.topic check_topic_owner(topic.owner, request.user) if request.method != 'POST': form = EntryForm(instance=entry) else: form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return redirect('learning_logs:topic', topic_id=topic.id) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'learning_logs/edit_entry.html', context)
def new_entry(request, pk): topic = Topic.objects.get(id=pk) if request.method != 'POST': form = EntryForm() else: form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return redirect('../') return render(request, 'learning_logs/new_entry.html', {'form': form, 'topic':topic})
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(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse("topic", args=[topic.id])) context = {"topic": topic, "entry": entry, "form": form} return render(request, "learning_logs/edit_entry.html", 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: # POST提交的数据,对数据进行处理 form = EntryForm(instance=entry,data=request.POST) #处理POST请求时,我们传递实参instance=entry和data=request.POST(见.),让Django根据既有条目对象创建一个表单实例,并根据request.POST中的相关数据对其进行修 if form.is_valid(): form.save() return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic.id])) context = {'entry':entry,'topic':topic,'form':form} return render(request, 'edit_entry.html',context)
def editentryview(request, entry_id): entry = Entry.objects.get(id=entry_id) topic = entry.topic if request.method != "POST": form = EntryForm(instance=entry) else: form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect("/Log/topic/") #if topic.owner != request.user: #raise Http404 context = { "entry" : entry, "topic" : topic, "form" : form, } return render(request, "editentry.html", context)
def addentryview(request, topic_id): """Add an entry to a topic""" topic = Topic.objects.filter( owner = request.user).get(id = topic_id) form = EntryForm(request.POST or None) if form.is_valid(): new_entry = form.save(commit = False) new_entry.topic = topic new_entry.save() form = EntryForm() context = { "form" : form, "topic" : topic, } return render(request, "entrycreate.html", context)
def new_entry(request, topic_id): """添加条目""" topic = Topic.objects.get(id=topic_id) if request.method != "POST": form = EntryForm() else: 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("topic", args=[topic_id])) context = {"topic": topic, "form": form} return render(request, "learning_logs/new_entry.html", context)
def edit_entry( request, entry_id): '''Edit an existing entry''' entry = Entry.objects.get(id= entry_id) topic = entry.topic if topic.owner != request.user: return redirect('learning_logs:topics') if request.method != "POST": #no data was submitted, fill the form with current data. form = EntryForm(instance=entry) else: #data was submitted, update the entry form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): #the content of the form is valid form.save() return redirect('learning_logs:topic', topic_id=topic.id) context = {'entry':entry, 'form':form, 'topic':topic} return render( request, 'learning_logs/edit_entry.html', 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: # post提交的数据,对数据进行处理 form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('learning_logs:topic', args=[topic.id])) context = {'entry': entry, 'topic': topic, 'form': form} return render(request, 'learning_logs/edit_entry.html', context)
def new_entry(request,topic_id): '''在待定的主题中添加新条目''' topic=Topic.objects.get(id=topic_id) if request.method!='POST': print("i am okla") #这里创建的新表单form有问题,导致加入的entry内容进不去,数据库也不行 form=EntryForm() else: form=EntryForm(data=request.POST) if form.is_valid(): new_entry=form.save(commit=False) new_entry.topic=topic new_entry.save() #这里的跳转跳不过去 return HttpResponseRedirect(reversed('learning_logs:topic',args=[topic_id])) context={'topic':topic,'form':form} return render(request,'learning_logs/new_entry.html',context)
def new_entry(request, topic_id): """Adds a new post on a specific topic.""" topic = get_object_or_404(Topic, id=topic_id) check_topic_owner(topic.owner, request.user) if request.method != 'POST': form = EntryForm() else: form = EntryForm(data=request.POST) if form.is_valid(): new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return redirect('learning_logs:topic', topic_id=topic_id) context = {'topic': topic, 'form': form} return render(request, 'learning_logs/new_entry.html', context)
def edit_entry(request, entry_id): """Edit an existing entry""" entry = Entry.objects.get(id=entry_id) topic = entry.topic check_topic_owner(request, topic) if request.method != 'POST': #Initial request, prefill form with current entry form = EntryForm(instance=entry) else: #Post data submitted, process data form = EntryForm(instance=entry, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('learning_logs:topic', args=[topic.id])) return render(request, 'edit_entry.txt', { 'entry': entry, 'topic': topic, 'form': form })
def new_entry(request,topic_id): """在特定的主题中添加新条目""" topic = Topic.objects.get(id=topic_id) if request.method != 'POST': #未提交数据,创建一个空表单 form = EntryForm() else: #post提交的数据,对数据进行处理 form = EntryForm(data=request.POST) if form.is_valid(): """调用save()时,我们传递了实参commit=False(见.),让Django创建一个新的条目对象,并将其存储到new_entry中,但不将它保存到数据库中。我们将new_entry的属性topic设置为在这个函数开头从数据库中获取的主题""" new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() #且不指定任何实参。这将把条目保存到数据库,并将其与正确的主题相关联。 return HttpResponseRedirect(reverse('learning_logs:topic',args=[topic_id])) context = {'topic':topic,'form':form} return render(request,'new_entry.html',context)
def new_entry(request, topic_id): '''Add a new entry to the topic with id == topic_id''' #get the topic topic = Topic.objects.get(id= topic_id) if topic.owner != request.user: return redirect('learning_logs:topics') if request.method != 'POST': #no data was submitted create empty form form = EntryForm() else: form = EntryForm(request.POST) if form.is_valid(): #dont add the entry to the database yet new_entry = form.save(commit=False) new_entry.topic = topic new_entry.save() return redirect('learning_logs:topic', topic_id=topic_id) #display a blank or invalid form context = {'topic':topic, 'form':form} return render( request, 'learning_logs/new_entry.html', context)