一般来说,我是Django/编程的初学者。 我的一个HTML页面上的重定向url按钮有问题。基本上,我有一个收集页面,可以添加植物。一旦添加了工厂,就可以向该工厂添加更多细节。我的CreateView已经创建,当我想添加按钮以向现有工厂添加详细信息时,打开工厂页面后,我会得到一个“未找到”错误的相反结果。 html代码中的粗体部分是错误的来源
models.py
class PlantDetail(models.Model):
"""This class contains various types of data of added plants"""
"""links the details to one plants"""
from datetime import date
plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
date_purchased = models.DateField(default=date.today)
notes = models.TextField()
def __str__(self):
"""Return the detail input from the user"""
return self.notes
def age(self):
"""Return the age of the plant in days"""
age_days = datetime.date.today() - date_purchased
return age_days.days
def get_absolute_url(self):
return reverse('detail', args=[int(self.plant_id)])
url.py
urlpatterns = [
# Landing page
path('', views.home, name='home'),
# Collection page
path('collection/', login_required(CollectionListView.as_view()), name='collection'),
# Detail page for a plant
path('collection/<int:plant_id>/', views.detail, name='detail'),
# New plant creation page
path('collection/add/', login_required(PlantCreateView.as_view()), name='plant-create'),
# Add details to an existing plant
path('collection/<int:plant_id>/add_details/', login_required(PlantDetailCreateView.as_view()), name='plantdetail-create'),
views.py
class PlantDetailCreateView(CreateView):
model = PlantDetail
fields = ['date_purchased', 'notes']
def form_valid(self, form):
form.instance.plant_id = self.kwargs['plant_id']
return super().form_valid(form)
details.html
{% extends "plntz_main/base.html" %}
{% block content %}
<h1> {{ plant_detail.name }} </h1>
<p><img src={{ plant_detail.image.url }}></p>
<p> Nickname: {{ plant_detail.nickname}}
<p> Category: {{ plant_detail.category }}
{% for detail in plant_detail.plantdetail_set.all %}
<p>Date purchased: {{ detail.date_purchased }} </p>
<p>Notes: {{ detail.notes }}</p>
{% endfor %}
<form>
<button formaction="{% url 'plantdetail-create' request.plant.id %}">Add details</button>
</form>
{% endblock content %}
编辑:键入错误后获取新错误: NoReverseMatch at/collection/1/ 找不到参数为“(“”,)”的“plantdetail create”的相反项。尝试了1种模式:[“收集/(?P<;工厂id>;[0-9]+)/添加详细信息/$”]
这是一个简单的拼写错误:
相关问题 更多 >
编程相关推荐