所以我有以下形式正在运作。:
<body>
<form method="POST" class = "incubatorBox" novalidate enctype="multipart/form-data">
{% csrf_token %}
<h2 class = "register-text"> Add Incubator</h2>
{{ form.as_p }}
<br>
<input class = "register-button" type="submit" name="" value="Register">
</form>
</body>
</html>
我有以下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 = {'incubator_id': 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
所以基本上这里发生的是第一种形式是在孵化器数据库中添加数据,并且在提交之后它必须将用户重定向到在models.py中的Details数据库中输入数据的表单。我的第一个表单正常工作并向数据库添加数据。但后来我收到了一个错误:
DoesNotExist at /incubators/19
Details matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/incubators/19
Django Version: 2.0.2
Exception Type: DoesNotExist
Exception Value:
Details matching query does not exist
我的views.py看起来像这样:
class AddIncubator(CreateView):
model = Incubators
fields = ['incubator_name', 'owner', 'city_location', 'description', 'logo']
class AddIncubatorDetails(CreateView):
model = Details
fields = ['incubator_name', 'owner', 'city_location', 'description', 'logo']
我想重定向到AddIncubatorDetails()中添加条目的表单 我也为此制作了一个表单模板。如果你想让我发帖,请告诉我。 以下是我的urls.py
from django.conf.urls import url
from . import views
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/$', views.AddIncubator.as_view(), name = 'add-incubator'),
url(r'incubators/add/(?P<incubator_id>[0-9]+)$', views.AddIncubatorDetails.as_view(), name='add-details'),# Adding Inc
]
答案 0 :(得分:0)
问题出在urls.py
,特别是此网址:
url(r'^incubators/(?P<incubator_id>[0-9]+)', views.details, name = 'details'), # shows details of incubators
在没有看到views.details
的情况下,我无法确切地告诉您问题是什么,但我怀疑您是否尝试在该视图中查找(尚未创建的)Details
对象。
Django的CreateView
具有success_url
属性,告诉它成功提交表单时要转到哪个URL。默认情况下,这将转到所创建对象的详细信息视图(请参阅:http://ccbv.co.uk/projects/Django/2.0/django.views.generic.edit/CreateView/#get_success_url)
我收集你的同时希望将他们引导至add-details
视图。在CreateView中,您可以定义success_url
属性:
class AddIncubator(CreateView):
success_url = reverse_lazy("add-details")
model = Incubators
fields = ['incubator_name', 'owner', 'city_location', 'description', 'logo']
我链接的网站http://ccbv.co.uk对于查找基于djangos类的视图的内部工作方式非常有用。