使用PK的URL重定向问题

时间:2017-09-10 20:18:28

标签: django django-models django-forms django-views django-urls

大家好我有以下错误的主键问题:django.urls.exceptions.NoReverseMatch:找不到'addteam'的反向。 'addteam'不是有效的视图函数或  模式名称。

我的应用很简单:用户创建项目然后使用按钮到达详细信息页面中的页面,以便使用邮件邀请用户...事情是我在创建项目后得到错误

这是我的代码:

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/(?P<pk>[0-9]+)/invite/$',views.create_invite, name='addteam'),
]

views.py:

from django.shortcuts import render
from django.views import generic
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import InviteForm
from invitations.models import Invitation
from .models import project
from django.core.urlresolvers import reverse

# Create your views here.

class HomePage(TemplateView):
    template_name= 'index.html'

class CandidateIndex(TemplateView):
    template_name= 'candidateIndex.html'

class HRIndex(TemplateView):
    template_name= 'HRindex.html'

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, pk):
    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' : self.pk} )
        else:
            print("Your form is not valid")
    else:
        invite_form = InviteForm()
    return render(request, 'team_invite.html', {'invite_form': invite_form})

models.py:

from django.db import models
from django.core.urlresolvers import reverse

class project(models.Model):
    project_name = models.CharField(max_length=150)
    created_at = models.DateTimeField(auto_now_add=True)

    def get_absolute_url(self):
        return reverse('website:ProjectDetails', kwargs = {'pk' : self.pk})

    def __str__(self):
        return self.project_name


class Team(models.Model):
    project = models.ForeignKey(project)
    team_name = models.CharField(max_length=150)

    def __str__(self):
        return self.team_name

project_details.html:

{% extends 'base.html' %}
{% block body %}
<div class="container">
  <div class="jumbotron">
    <h2>Welcome to your Project {{project.project_name}} Detail page</h2>
  </div>
  <div class="invite-team">
      <a href="{% url 'website:addteam'%}">GOGO</a>
  </div>
</div>




{% endblock%}

错误: 在模板/Users/raphaelbendenoun/Documents/SoftScores_project_Final/templates/base.html中,第8行的错误 未找到'addteam'的反向。 'addteam'不是有效的视图函数或模式名称。

- &GT;第8行是靴子链接...

1 个答案:

答案 0 :(得分:0)

以下行表示您的视图正在接受项目模型的主键。

url(r'^project/(?P<pk>[0-9]+)/invite/$',views.create_invite, name='addteam'),

但是,您的观点并未表明您在其中使用了项目模型。

def create_invite(request, pk):

所以改变

 def create_invite(request, pk): 

 def create_invite(request):

url(r'^project/(?P<pk>[0-9]+)/invite/$',views.create_invite, name='addteam')

url(r'^project/invite/$',views.create_invite, name='addteam') 

希望您的模板能够正常运行。

如果您需要将主键传递给视图,则需要在模板中执行

<a href="{% url 'website:addteam' pk_key_value %}">GOGO</a>

其中pk_key_value应该是带有模型对象主键的变量。

以下链接将为您提供更清晰的信息。 https://docs.djangoproject.com/en/1.11/topics/http/urls/

也用于发送电子邮件 https://docs.djangoproject.com/en/1.11/topics/email/

您似乎没有'从django.core.mail'导入