我如何 link 我在 Django 模板中的列表视图和详细信息视图,以便当我单击列表时它会显示详细信息
how do i link my listview and detailsview in django templates such that when i click the list it then shows me the details
"im setting up my django-templates of a project o show schoolevents and job listings, i want its response to show me details of a list once i click the item on the list -can some one help am a newbie tp programming"
我已经尝试将 slugfield 添加到我的模型中,更改了几次 url,但我仍然无法达到我的目标
models.py
class Events(models.Model):
title = models.CharField(max_length=80)
host = models.CharField(max_length=80)
venue = models.CharField(max_length=80)
event_image = models.ImageField()
details = models.TextField()
posted_at = models.DateTimeField(auto_now_add=True)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
contacts = models.CharField(max_length=80)
sponsors = models.CharField(max_length=80, null=True, blank=True)
slug = models.SlugField(max_length=150, null=True, blank=True)
class Meta:
ordering = ('-posted_at',)
views.py
class EventsListView(ListView):
model = Events
form_class = EventsForm
context_object_name = 'all_events'
template_name = 'events_list.html'
success_url = reverse_lazy('list_events')
def get_queryset(self):
qs = super(EventsListView, self).get_queryset()
return qs
class DetailEventsView(DetailView):
model = Events
def render_to_response(self, context, **response_kwargs):
if self.request.is_ajax():
return JsonResponse('Your comment has been uploaded!',safe=False, **response_kwargs)
else:
return super(DetailView,self).render_to_response(context, **response_kwargs)
def get_context_data(self, **kwargs):
context = super(Events, self).get_context_data(**kwargs)
data = self.kwargs['name']
context['object'] = Events.objects.filter(events_id=data)
return context
url.py
urlpatterns = [
url(r'^create-events/$', CreateEventsView.as_view(),
name='create_events'),
url(r'^list-events/$', EventsListView.as_view(),
name='list_events'),
url(r'^(?P<events_id>\d+)$', DetailEventsView.as_view(),
name='detail_events'),
]
模板
<div>
{% for events in all_events %}
<li>
<a href="{% url 'posts:list_events' %}"> {{ events.title }}
</a>
</li>
<a href="{% url 'posts:detail_events' %}" class="btn btn-default">
{% trans '{{ events.title }}' %}</a>
{% endfor %}
</div>
Reverse for 'detail_events' with no arguments not found. 1 pattern(s) tried: ['(?P<events_id>\d+)$']
好吧,我想了很久但只是为了帮助像我这样的新手,问题是模板,在 events_list 链接到 event_details 的部分就像
<a href="{% url 'posts:detail_events' %}" class="btn btn-default">
但应该是这样
<a href="{% url 'posts:detail_events' events.id %}"> {% trans '{{ events.title }}' %}</a>
详细信息可以是
<h1 class="mt-4 text-center">{{ events.title }}</h1>
host : <p>{{ events.host }}</p>
......
"im setting up my django-templates of a project o show schoolevents and job listings, i want its response to show me details of a list once i click the item on the list -can some one help am a newbie tp programming"
我已经尝试将 slugfield 添加到我的模型中,更改了几次 url,但我仍然无法达到我的目标
models.py
class Events(models.Model):
title = models.CharField(max_length=80)
host = models.CharField(max_length=80)
venue = models.CharField(max_length=80)
event_image = models.ImageField()
details = models.TextField()
posted_at = models.DateTimeField(auto_now_add=True)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
contacts = models.CharField(max_length=80)
sponsors = models.CharField(max_length=80, null=True, blank=True)
slug = models.SlugField(max_length=150, null=True, blank=True)
class Meta:
ordering = ('-posted_at',)
views.py
class EventsListView(ListView):
model = Events
form_class = EventsForm
context_object_name = 'all_events'
template_name = 'events_list.html'
success_url = reverse_lazy('list_events')
def get_queryset(self):
qs = super(EventsListView, self).get_queryset()
return qs
class DetailEventsView(DetailView):
model = Events
def render_to_response(self, context, **response_kwargs):
if self.request.is_ajax():
return JsonResponse('Your comment has been uploaded!',safe=False, **response_kwargs)
else:
return super(DetailView,self).render_to_response(context, **response_kwargs)
def get_context_data(self, **kwargs):
context = super(Events, self).get_context_data(**kwargs)
data = self.kwargs['name']
context['object'] = Events.objects.filter(events_id=data)
return context
url.py
urlpatterns = [
url(r'^create-events/$', CreateEventsView.as_view(),
name='create_events'),
url(r'^list-events/$', EventsListView.as_view(),
name='list_events'),
url(r'^(?P<events_id>\d+)$', DetailEventsView.as_view(),
name='detail_events'),
]
模板
<div>
{% for events in all_events %}
<li>
<a href="{% url 'posts:list_events' %}"> {{ events.title }}
</a>
</li>
<a href="{% url 'posts:detail_events' %}" class="btn btn-default">
{% trans '{{ events.title }}' %}</a>
{% endfor %}
</div>
Reverse for 'detail_events' with no arguments not found. 1 pattern(s) tried: ['(?P<events_id>\d+)$']
好吧,我想了很久但只是为了帮助像我这样的新手,问题是模板,在 events_list 链接到 event_details 的部分就像
<a href="{% url 'posts:detail_events' %}" class="btn btn-default">
但应该是这样
<a href="{% url 'posts:detail_events' events.id %}"> {% trans '{{ events.title }}' %}</a>
详细信息可以是
<h1 class="mt-4 text-center">{{ events.title }}</h1>
host : <p>{{ events.host }}</p>
......