Html
{% for well_instance, values in well_info %}
<tr>
{% for value in values %}
<td><a href="{% url 'eric_base:contextual' api=well_instance.api %}">{{ value }}</a></td>
{% endfor %}
</tr>
{% endfor %}
views.py
class WellList_ListView(ListView):
template_name = 'well_list.html'
context_object_name = 'well_info'
model = models.WellInfo
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# get string representation of field names in list
context['fields'] = [field.name for field in models.WellInfo._meta.get_fields()]
# nested list that has all objects' all attribute values
context['well_info_values'] = [[getattr(instance, field) for field in context['fields']] for instance in context['well_info']]
# includes well instance objects & values string list for each well
context['well_info'] = zip([instance for instance in context['well_info']], context['well_info_values'])
return context
class ContextualMainView(TemplateView):
template_name = 'contextual_main.html'
eric_base/urls.py
app_name = 'eric_base'
urlpatterns = [
re_path(r'^(?P<api>[\w-]+)/$', base_views.ContextualMainView.as_view(), name='contextual'),
]
project/urls.py
urlpatterns = [
path('well_list/', include([
re_path(r'^$', views.WellList_ListView.as_view(), name='well_list'),
re_path(r'^create/$', views.AddWell_CreateView.as_view(), name='create'),
re_path(r'^contextual/$', include('eric_base.urls')),
]))
models.py
# Create your models here.
class WellInfo(models.Model):
api = models.CharField(max_length=100, primary_key=True)
well_name = models.CharField(max_length=100)
status = models.CharField(max_length=100)
phase = models.CharField(max_length=100)
region = models.CharField(max_length=100)
start_date = models.CharField(max_length=100)
last_updates = models.CharField(max_length=100)
def __str__(self):
return self.well_name
Error Message
NoReverseMatch at /well_list/
Reverse for 'contextual' with keyword arguments '{'api': '9933124422'}' not found. 1 pattern(s) tried: ['well_list\\/contextual/$(?P<api>[\\w-]+)/$']
我真的不喜欢这个 . 从我得到 {'api': '9933124422'}
的非空值的事实来看,我认为我正确地从 {% url 'eric_base:contextual' api=well_instance.api %}
注入了字典,但它仍然给我一个错误 . 请帮忙!
1 回答
您可以将
TemplateView
更改为DetailView
,因为TemplateView
没有get_object
方法 . 例如在你的情况下:至;
See also this docs:
http://ccbv.co.uk/projects/Django/2.0/django.views.generic.base/TemplateView/
http://ccbv.co.uk/projects/Django/2.0/django.views.generic.detail/DetailView/
在讨论之后,实际问题就在这里 . 字符
$
应该用于最终网址,如果您有另一个包含网址,则不应使用该字符 .至;