为什么我在Django中看到以下错误?

时间:2018-03-17 16:36:26

标签: django

所以,我的Django项目中有一个用户表单。提交表单后,我想将用户重定向到另一个表单(有关其他详细信息)。但我看到以下错误

NoReverseMatch at /incubators/add-incuabtor/
Reverse for 'details' with keyword arguments '{'pk': 15}' not found. 1 pattern(s) tried: ['incubators/(?P<incubator_id>[0-9]+)']

Request Method:     POST
Request URL:    http://127.0.0.1:8000/incubators/add-incuabtor/

我有以下网址格式:

app_name = 'main'

urlpatterns = [
    url(r'^home/', views.home, name='home'),    # Home page
    url(r'incubators/$', views.incubators, name='incubators'),    # Incubator list page
    url(r'about/', views.about, name='about'),          # Websie about page
    url(r'results', views.result, name = 'result'),         # For search function
    url(r'incubators/(?P<incubator_id>[0-9]+)', views.details, name = 'details'),      # shows details of incubators
    url(r'incubators/add-incuabtor/$', views.AddIncubator.as_view(), name = 'add-incubator'),     # Adding Inc
    url(r'/add-details/', views.AddDetails.as_view(), name = 'add-details'), #for additional details
]

以下是我的models.py

class Incubators(models.Model):      # These are our database files for 

the Incubator Portal
    incubator_name = models.CharField(max_length=30)
    owner = models.CharField(max_length=30)
    city_location = models.CharField(max_length=30)
    description = models.TextField(max_length=100)
    logo = models.FileField()
    verify = models.BooleanField(default = False)

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

    def __str__(self):                  # Displays the following  stuff when a query is made
      return self.incubator_name + '-' + self.owner 

class Details(models.Model):
    incubator = models.ForeignKey(Incubators, on_delete = models.CASCADE, related_name='banana_pudding')    
    inc_name = models.CharField(max_length = 30)
    inc_img = models.FileField()
    inc_contact = models.CharField(max_length = 600, default = "Enter all available means of contacting")
    inc_details = models.TextField(max_length= 2500)
    inc_address = models.TextField(max_length = 600, default = "Address")
    inc_doc = models.FileField()
    inc_policy = models.FileField()

    def __str__(self):
        return self.inc_name

我有以下views.py

class AddIncubator(CreateView):
    model = Incubators
    fields = ['incubator_name', 'owner', 'city_location', 'description', 'logo']

class AddDetails(CreateView):
    model = Details
    field = ['incubator', 'inc_name']

1 个答案:

答案 0 :(得分:1)

问题在于你的网址。 在urls.py中,您的详细信息是

url(r'incubators/(?P<incubator_id>[0-9]+)', views.details, name = 'details'),

将其更改为

url(r'incubators/(?P<pk>[0-9]+)', views.details, name = 'details'),

或者您可以在模型中更改反向网址:

def get_absolute_url(self):
        return reverse('main:details', kwargs={'incubator_id': self.pk})