【发布时间】:2018-12-28 04:18:54
【问题描述】:
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
错误信息
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 %} 注入了字典,但它仍然给我一个错误。请帮忙!
【问题讨论】:
-
让我们看看你的
ContextualMainView -
@SancaKembang 抱歉,感谢您的指出。我刚刚添加了它
标签: django django-models django-templates django-views django-urls