Python 如何在Django中将slug作为url传递
我试图在Url中传递slug,但我无法这样做。 slug的格式类似于if have text:- text:-这是Stackoverflow 斯拉格:-这是stackoverflow 但我无法做到这一点。 我的模型:-Python 如何在Django中将slug作为url传递,python,django,Python,Django,我试图在Url中传递slug,但我无法这样做。 slug的格式类似于if have text:- text:-这是Stackoverflow 斯拉格:-这是stackoverflow 但我无法做到这一点。 我的模型:- from __future__ import unicode_literals from django.db import models class EmailTemplates(models.Model): id = models.IntegerField(db_
from __future__ import unicode_literals
from django.db import models
class EmailTemplates(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
subject = models.CharField(db_column="subject",max_length=255)
slug = models.CharField(db_column="slug",unique=True, max_length=255)
content = models.TextField(db_column="content", help_text="")
createdAt = models.DateTimeField(db_column='createdAt',auto_now=True, help_text="")
modifiedAt = models.DateTimeField(db_column='modifiedAt',auto_now=True, help_text="")
updatedBy = models.IntegerField(db_column='updatedBy', help_text="")
class Meta:
managed = False
db_table = 'email_templates'
我正在尝试的url是:-
url(r'^email/edit/(?P<email_template_id>[\w-]+)/$', email_managment.email_send, name='email-edit')
正在发生的错误是
以10为基数的int()的文本无效:“hello Taked of愚人”
如果有语法错误,请忽略。
提前感谢因为您不会显示全部错误,我们必须猜测发生了什么 有一件事很明显你做错了。即使您将要传递的参数称为
email\u template\u id
,但它不是一个id,而是一个slug。但是当您查询模型时,您是按ID而不是按slug进行查询的
在使用EmailTemplates.objects.get()
的两个地方,都应该使用slug=email\u template\u id
,而不是id=…
。如果你调用了你的参数,它实际上是一个slug而不是id,这会更明显
(另外请注意,如果您只是将对象作为
实例
参数传入表单实例化,则可以使您的模型表单更新现有对象以及创建新对象。这将使您首先否则
块不必要。但即使您选择不这样做,您也应该始终从form.clea获取数据。)ned_数据
而不是直接来自请求。POST
)什么意思,你无法通过slug?你是怎么尝试的?当你这样做时发生了什么?你得到了什么错误?错误:-无效的int()文本对于base 10:“hello-Take-of-Dulls”,您需要显示完整的回溯,以便我们知道错误来自何处。您还应该显示您的模型。我已经共享了我的模型,这是我得到的唯一错误,因为您有意用无意义的try/except块隐藏其余错误。删除该错误,然后让Django向您显示完整的错误错误。
@login_required
@csrf_exempt
def email_send(request,email_template_id):
try:
form = emailTemplates(request.POST or None)
edit_email_end = 'add'
email_template_obj = {}
if request.method == 'POST':
if form.is_valid():
subject = request.POST.get('subject')
lowercase = subject.lower()
slug = lowercase.replace(" ","-")
print slug
content = request.POST.get('content')
if email_template_id is None or email_template_id == '':
add_email_template_entry = EmailTemplates(subject=subject, content=content,slug=slug)
add_email_template_entry.save(using="cms")
messages.success(request, 'Successfully added to the email template page')
elif email_template_id:
email_template_obj = EmailTemplates.objects.using("cms").get(id=email_template_id)
email_template_obj.subject = subject
email_template_obj.content = content
email_template_obj.slug = slug
email_template_obj.save(using="cms")
messages.success(request, 'Successfully update to the email templates')
return redirect('cms:email-list')
if email_template_id is not None:
edit_email_end = 'edit'
email_template_obj = EmailTemplates.objects.using("cms").get(id=email_template_id)
return render(request, 'templates/email_managment/add_email.html',{
'edit_email_end': edit_email_end,'email_template_obj':email_template_obj})
except Exception as e:
print e
raise Http404