在我的 Django 实现中,我创建了一个模型,该模型将显示我存储在网站管理员中的信息。我设法创建公共页面以在模型中具有名称属性的链接,但是当我尝试从链接转到 viewpara 页面后在 viewpara 页面上执行相同操作时,我无法获得模型的任何属性出现在此页面上,我很困惑为什么。
模型.py
class Parasite(models.Model):
name = models.CharField(max_length=128, unique=True)
slug = models.SlugField(max_length=100, unique=True)
description = models.TextField(max_length=100000)
image = models.ImageField(upload_to='parasite_images', default='default/default.jpg', blank=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Parasite, self).save(*args, **kwargs)
def __str__(self):
return self.name
视图.py
def public(request):
c = Parasite.objects.all()
context = {'c': c}
return render(request, 'parasites_app/public.html',context)
def view_parasite(request,parasite_name_slug):
context_dict = {}
try:
parasite = Parasite.objects.get(slug=parasite_name_slug)
context_dict['parasites'] = parasite
except Parasite.DoesNotExist:
context_dict['parasites'] = None
return render(request, 'parasites_app/viewpara.html', context = context_dict)
网址.py
path('login/', views.user_login, name='login'),
path('logout/', views.user_logout, name='logout'),
path('admin/', admin.site.urls),
path('', views.public, name='public'),
path('public/', views.public, name='public'),
path('private/', views.logged_in_content, name='private'),
path('viewpara/',views.public,name='viewpara'),
path('public/<slug:parasite_name_slug>/',views.view_parasite, name='view_parasite'),
path('<slug:post_name_slug>/', views.show_post, name='show_post'),
公共.html
<!DOCTYPE html>
{% extends 'parasites_app/base.html' %}
{% load static %}
{% block content_block %}
<h1>Public Page</h1>
<h2>Information based on series of Parasites</h2>
{% for p in c %}
<a href="{% url 'view_parasite' p.slug %}">{{p.name}}</a>
{% endfor %}
<h3>Protozoa</h3>
<p>Protozoa are microscopic, one-celled organisms that can be free-living or parasitic in nature. They are able to multiply in humans, which contributes to their survival and also permits serious infections to develop from just a single organism. Transmission of protozoa that live in a human’s intestine to another human typically occurs through a fecal-oral route (for example, contaminated food or water or person-to-person contact). Protozoa that live in the blood or tissue of humans are transmitted to other humans by an arthropod vector (for example, through the bite of a mosquito or sand fly).</p>
<p></p>
<h3>Helminths</h3>
<p>Helminths are large, multicellular organisms that are generally visible to the naked eye in their adult stages. Like protozoa, helminths can be either free-living or parasitic in nature. In their adult form, helminths cannot multiply in humans.</p>
</h2>
{% endblock %}
viewpara.html
<!DOCTYPE html>
{% extends 'parasites_app/base.html' %}
{% load static %}
{% block content_block %}
{% for p in c %}
<h3>{{p.name}}</h3>
{% endfor %}
{% endblock %}