几天前我决定学习Python / Django并在我的一个私人项目中练习使用它。我已经在builidng'polls'应用程序中完成了djangoproject教程1-6,现在又开始自己做了。
简单问题 - 我想要一个端点来查看,创建,编辑和删除“交易”。
到目前为止,我在 views.py
中有以下内容from django.views import generic
from django.http import HttpResponseRedirect
from django.views.generic.edit import CreateView
from django.core.urlresolvers import reverse
from .models import Deal
class IndexView(generic.ListView):
context_object_name = 'latest_deals_list'
def get_queryset(self):
return Deal.objects.order_by('-created_date')[:10]
class DetailView(generic.DetailView):
model = Deal
class DealCreate(CreateView):
model = Deal
fields = ['title', 'url']
def post(self, *args, **kwargs):
title = self.request.POST["title"]
url = self.request.POST["url"]
try:
deal = Deal(tite=title, url = url)
deal.save()
except Exception as e:
print e
return HttpResponseRedirect(reverse('deals:detail', args=(deal.id,)));
和我的 urls.py 就像这样:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^create/$', views.DealCreate.as_view(), name='create')
]
一切正常,但我很难理解如何将其组织成更简洁的API式结构。我可以在路由中只有/处理,然后使用不同的模板单独实现所有方法GET / POST / PUT / DELETE吗?我会在indexView中执行此操作,并且只在其他视图中打印表单吗?我觉得在/ create中使用POST方法感觉不对,或者我在思考这个问题。
非常感谢任何反馈。
答案 0 :(得分:0)
我意识到最好使用基于函数的方法与ModelForm而不是GenericViews。我使用表单验证工作的更新代码如下所示:
from django.views import generic
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Deal
from .forms import DealForm
class IndexView(generic.ListView):
context_object_name = 'latest_deals_list'
def get_queryset(self):
return Deal.objects.order_by('-created_date')[:10]
class DetailView(generic.DetailView):
model = Deal
def add_model(request):
form = DealForm(request.POST or None)
if form.is_valid():
deal = form.save()
return HttpResponseRedirect(reverse('deals:detail', args=(deal.id,)));
return render(request, "deals/deal_form.html", {'form': form, 'title': 'Add New Deal'})
def update_model(request, pk):
deal = get_object_or_404(Deal, pk=pk)
form = DealForm(request.POST or None, instance=deal)
if form.is_valid():
deal = form.save()
return HttpResponseRedirect(reverse('deals:detail', args=(deal.id,)));
return render(request, "deals/deal_form.html", {'form': form, 'title': 'Editing "' + deal.title + '"'})
def delete_model(request, pk):
deal = get_object_or_404(Deal, pk=pk)
if request.method == 'POST':
deal.delete()
return HttpResponseRedirect(reverse('deals:index'));
return render(request, "deals/deal_confirm_delete.html", {'object': deal, 'title': 'Confirm deleting "' + deal.title + '"'})
urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^create/$', views.add_model, name='create'),
url(r'^(?P<pk>[0-9]+)/edit/$', views.update_model, name='update'),
url(r'^(?P<pk>[0-9]+)/delete/$', views.delete_model, name='delete')
]
和forms.py:
from django.forms import ModelForm
from .models import Deal
class DealForm(ModelForm):
class Meta:
model = Deal
fields = ['title', 'url']