我想创建一个系统。对于这个系统,我需要耐心地创建表格。当我单击按钮时,可以保存患者,但是按下按钮后会出现错误页面。
找不到带有关键字参数'{'slug':'James'}'的'create'。尝试了1个模式:['create / $']
(填写表单时,我将James写入 title 字段),甚至我在mySql数据库中也写James,title列就像id。 1,2,3,...等等不是詹姆斯...
我的错误在哪里?非常感谢。
urls.py
app_name = "patients"
urlpatterns = [
re_path('patients/', views.PatientList.as_view(), name='patients_list'),
url(r'^create/$', patient_create, name='patient_create'),
path('create/<slug:slug>/', views.PatientsDetail.as_view(), name='patients_detail'),
]
models.py
class newPatients(models.Model):
title = models.CharField(max_length=100)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
created_date = models.DateTimeField(default=timezone.now)
dept = models.TextField()
address = models.TextField()
phone = models.CharField(max_length=15)
notes = RichTextField(verbose_name="notes")
slug = models.SlugField(max_length=100, unique=True, editable=False)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.first_name
def get_create_url(self):
return reverse('patients:patient_create', kwargs={'slug': self.slug})
def get_unique_slug(self):
slug = slugify(self.title.replace('ı', 'i'))
unique_slug = slug
counter = 1
while newPatients.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, counter)
counter += 1
return unique_slug
def get_absolute_url(self):
return reverse('patients:patient_create', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
self.slug = self.get_unique_slug()
return super(newPatients, self).save(*args, **kwargs)
class Meta:
ordering = ['-created_date']
回溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/create/
Django Version: 2.2.9
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'patients',
'widget_tweaks',
'crispy_forms',
'ckeditor']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\edeni\senior\pharsys\patients\views.py" in patient_create
31. return redirect(newPatients)
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\shortcuts.py" in redirect
58. return redirect_class(resolve_url(to, *args, **kwargs))
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\shortcuts.py" in resolve_url
134. return to.get_absolute_url()
File "C:\Users\edeni\senior\pharsys\patients\models.py" in get_absolute_url
40. return reverse('patients:create', kwargs={'slug': self.slug})
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\urls\base.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\edeni\senior\myenv\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
673. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /create/
Exception Value: Reverse for 'create' with keyword arguments '{'slug': 'james'}' not found. 1 pattern(s) tried: ['create/$']
答案 0 :(得分:0)
读取错误(检查您的自己的代码中的行):
File "C:\Users\edeni\senior\pharsys\patients\views.py" in patient_create
31. return redirect(newPatients)
...
File "C:\Users\edeni\senior\pharsys\patients\models.py" in get_absolute_url
40. return reverse('patients:create', kwargs={'slug': self.slug})
它告诉您错误在您的get_absolute_url
方法中(从重定向中调用)。并且它告诉您不能使用关键字参数'{'slug':'james'}'反转'create'。好吧,现在很清楚,您的网址格式create
没有参数'slug'。可能您不是要“创建”(对于详细视图会很奇怪),而是“ Patients_detail”?