Django形式和重定向

时间:2017-09-10 12:58:42

标签: django django-models django-forms django-templates django-rest-framework

我的代码如下: 在views.py中:

class ProjectDetailView(generic.DetailView):
    model = project
    template_name = 'project_details.html'

class ProjectCreate(CreateView):
    model = project
    fields = ['project_name']
    template_name = 'project_form.html'

def create_invite(request):
    if request.method == "POST":
        invite_form = InviteForm(data=request.POST)
        if invite_form.is_valid():
            email1 = invite_form.cleaned_data['email1']
            email2 = invite_form.cleaned_data['email2']
            email3 = invite_form.cleaned_data['email3']
            email4 = invite_form.cleaned_data['email4']
            email5 = invite_form.cleaned_data['email5']
            for i in invite_form.cleaned_data:
                invite = Invitation.create(i)
                invite.send_invitation(request)
            print("The mail was went")
            return reverse('website:ProjectDetails', kwargs = {'pk' : project.pk} )
        else:
            print("Your form is not valid")
    else:
        invite_form = InviteForm()
    return render(request, 'team_invite.html', {'invite_form': invite_form})

在form.py中:

from django import forms


class InviteForm(forms.Form):
    email1 = forms.EmailField(label='Email 1')
    email2 = forms.EmailField(label='Email 2')
    email3 = forms.EmailField(label='Email 3')
    email4 = forms.EmailField(label='Email 4')
    email5 = forms.EmailField(label='Email 5')

urls.py:

from django.conf.urls import url
from website import views

app_name = 'website'

urlpatterns = [
    url(r'^candidateIndex/$', views.CandidateIndex.as_view(), name='candidate_index'),
    url(r'^HRcreate/$', views.ProjectCreate.as_view(), name='HR_create'),
    url(r'^project/(?P<pk>[0-9]+)/$',views.ProjectDetailView.as_view(), name='ProjectDetails'),
    url(r'^project/add/$',views.ProjectCreate.as_view(), name='addproject'),
    url(r'^invite/$',views.create_invite, name='addteam'),
]

您可以看到:我有一个向用户发送电子邮件邀请的表单。使用该表格我有两个问题:

1)似乎不是把输入中的值作为电子邮件,它似乎只采用了Email1或Email2等变量。

2)当我尝试使用项目pk重定向到详细信息页面时出现错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'ProjectDetails' with keyword arguments '{'pk': <property object at 0x10ecf79a8>}' not found. 1 pattern(s) tried: ['website/project/(?P<pk>[0-9]+)/$']

有什么想法吗?

0 个答案:

没有答案