Python 向Django URL传递多个参数-意外的关键字参数
如何解决此Python 向Django URL传递多个参数-意外的关键字参数,python,django,django-views,Python,Django,Django Views,如何解决此NoReverseMatch错误 上下文 我想尽可能多地记录这个问题,以便能够详细说明问题的根源 我有一个名为住宿优惠的模型,用于创建住宿优惠并获取其详细信息 class LodgingOffer(models.Model): # Foreign Key to my User model created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
NoReverseMatch
错误
上下文
我想尽可能多地记录这个问题,以便能够详细说明问题的根源
我有一个名为住宿优惠
的模型,用于创建住宿优惠并获取其详细信息
class LodgingOffer(models.Model):
# Foreign Key to my User model
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
ad_title = models.CharField(null=False, blank=False,
max_length=255, verbose_name='Título de la oferta')
slug = models.SlugField(max_length=100, blank=True)
# ... Another fields ...
def __str__(self):
return "%s" % self.ad_title
def get_absolute_url(self):
return reverse('host:detail', kwargs = {'slug' : self.slug })
# I assign slug to offer based in ad_title field,checking if slug exist
def create_slug(instance, new_slug=None):
slug = slugify(instance.ad_title)
if new_slug is not None:
slug = new_slug
qs = LodgingOffer.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" % (slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
# Brefore to save, assign slug to offer created above.
def pre_save_article_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
pre_save.connect(pre_save_article_receiver, sender=LodgingOffer)
对于这个模型,我有一个名为
HostingOfferDetailView
的详细视图
我想追求的一个重要目标是,在住宿优惠
对象的详细视图中,我应该能够联系该优惠的所有者(对象住宿优惠
-创建该优惠的用户-),以便其他感兴趣的用户可以申请该优惠
我希望在LodingOffer
对象的详细视图中,能够联系报价所有者(对象LodingOffer
-创建该报价的用户-),以便其他感兴趣的用户可以申请该报价
为此,我使用了联系\u所有者\u offer()
功能,在这里我向此优惠的所有者发送电子邮件
我正在使用HostingOfferDetailView
详细视图进行所有操作,如下所示:
class HostingOfferDetailView(DetailView):
model = LodgingOffer
template_name = 'lodgingoffer_detail.html'
context_object_name = 'lodgingofferdetail'
def get_context_data(self, **kwargs):
context = super(HostingOfferDetailView, self).get_context_data(**kwargs)
user = self.request.user
# Get the related data offer
#lodging_offer_owner = self.get_object()
lodging_offer_owner_full_name = self.get_object().created_by.get_long_name()
lodging_offer_owner_email = self.get_object().created_by.email
lodging_offer_title = self.get_object().ad_title
user_interested_email = user.email
user_interested_full_name = user.get_long_name()
# Send to context email of owner offer and user interested in offer
context['user_interested_email'] = user_interested_email
context['lodging_offer_owner_email'] = lodging_offer_owner_email
# Send the data (lodging_offer_owner_email
# user_interested_email and lodging_offer_title) presented
# above to the contact_owner_offer function
contact_owner_offer(self.request, lodging_offer_owner_email, user_interested_email, lodging_offer_title)
return context
Mycontact\u owner\u offer()
函数接收这些offer参数,并向offer的所有者或发布者发送电子邮件,如下所示:
def contact_owner_offer(request, lodging_offer_owner_email, user_interested_email, lodging_offer_title):
user = request.user
if user.is_authenticated:
# print('Send email')
mail_subject = 'Interest in your offer'
context = {
'lodging_offer_owner_email': lodging_offer_owner_email,
# User owner offer - TO send email message
'offer': lodging_offer_title,
# offer for which a user asks
'user_interested_email': user_interested_email,
# Interested user offer
'domain': settings.SITE_URL,
'request': request
}
message = render_to_string('contact_user_own_offer.html', context)
#to_email = lodging_offer_owner.email,
send_mail(mail_subject, message, settings.DEFAULT_FROM_EMAIL,
[lodging_offer_owner_email], fail_silently=True)
我这样做是为了测试,到目前为止,一切都是我想要的,结果是,当我输入详细报价对象LodingOffer
的URL时,会向该报价的所有者发送一封电子邮件
我希望优惠详细信息模板有一个按钮,其中包含“联系优惠所有者”,任何按下该按钮的用户都会向优惠所有者发送电子邮件
为此,我为contact\u owner\u offer()
函数定义了一个URL,它在我的模板中的按钮的href
属性上有一个链接
URL(根据我的理解,这就是我的疑问和问题的原因所在)我根据contact\u owner\u offer()
函数接收的参数数量定义了它
这意味着我的URL必须接收:
- 优惠所有者的电子邮件地址
- 对报价感兴趣的用户的电子邮件地址
- 报价的标题,尽管我对此感到遗憾 我给你发了那篇文章的开头,我不知道这是否正确
url(r'^contact-to-owner/(?P<email>[\w.@+-]+)/from/'
r'(?P<interested_email>[\w.@+-]+)/(?P<slug>[\w-]+)/$',
contact_owner_offer, name='contact_owner_offer'),
我的追踪到了
Traceback:
File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
42. response = get_response(request)
File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /host/contact-to-owner/botibagl@gmail.com/from/botibagl@gmail.com/apartacho/
Exception Value: contact_owner_offer() got an unexpected keyword argument 'email'
我不明白的是,它告诉我,我的URL不会等待一个名为email
的参数,在这个参数中,我通过模板中的按钮传递参数email=lotting\u offer\u owner\u email
我感谢你的指导
致意
更新 根据和建议,我稍微更改了我的URL正则表达式,指定了我要传递的关键字参数的名称。URL将以这种方式保留:
url(r'^contact-to-owner/(?P<lodging_offer_owner_email>[\w.@+-]+)/from/'
r'(?P<interested_email>[\w.@+-]+)/(?P<slug>[\w-]+)/$',
contact_owner_offer,
name='contact_owner_offer'
),
一般追溯如下:
File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /host/lodging-offer/apartacho/
Exception Value: Reverse for 'contact_owner_offer' with arguments '()' and keyword arguments '{'email': 'botibagl@gmail.com', 'interested_email': 'botibagl@gmail.com', 'slug': 'apartacho'}' not found. 1 pattern(s) tried: ['host/contact-to-owner/(?P<lodging_offer_owner_email>[\\w.@+-]+)/from/(?P<interested_email>[\\w.@+-]+)/(?P<slug>[\\w-]+)/$']
文件“/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site packages/django/url/resolvers.py”在带有前缀的
392(查找视图、参数、kwargs、len(模式)、模式)
例外类型:NoReverseMatch at/host/Lotting offer/apartacho/
异常值:与参数“()”和关键字参数“{'email':”相反的“contact_owner_offer”botibagl@gmail.com“,“感兴趣的电子邮件”:”botibagl@gmail.com“,'slug':'apartacho'}”未找到。尝试了1个模式:[“主机/联系到所有者/(?P[\\w.@+-]+)/从/(?P[\\w.@+-]+)/(?P[\\w-]+)/$”]
如何解决此
NoReverseMatch
错误 在URL正则表达式中,您必须指定要传递的kyword arg的名称:
url(r'^contact-to-owner/(?P<email1>[\w.@+-]+)/from/' r'(?P<email1>[\w.@+-]+)/(?P<email3>[\w-]+)/$', contact_owner_offer, name='contact_owner_offer'),
url(r'^contact to owner/(?P[\w.+-]+)/from/'r'(?P[\w.+-]+)/(?P[\w-]+)/$),联系业主报价,姓名='联系业主报价',
这是指:
在URL正则表达式中,您必须指定要传递的kyword arg的名称:
url(r'^contact-to-owner/(?P<email1>[\w.@+-]+)/from/' r'(?P<email1>[\w.@+-]+)/(?P<email3>[\w-]+)/$', contact_owner_offer, name='contact_owner_offer'),
url(r'^contact to owner/(?P[\w.+-]+)/from/'r'(?P[\w.+-]+)/(?P[\w-]+)/$),联系业主报价,姓名='联系业主报价',
这是指:
为什么不使用带有get参数的简单url
e、 g.“联系到所有者/?所有者电子邮件=xxx&用户电子邮件=xxx&标题=xxx”为什么不使用带有get参数的简单url
e、 g.“联系到所有者/?所有者电子邮件=xxx&用户电子邮件=xxx&标题=xxx”您是否试图从一个视图调用另一个视图?或者只是想调用一个函数?如果你只想调用一个函数,不必在ulrs中列出它。首先,在GET上发送电子邮件之类的任何操作都是一个非常糟糕的主意。其次,你在这里发布的URL不会给出这个错误;相反,您会得到一个反向匹配错误,因为您正在将关键字args传递给
{%url%}
标记。最后,不要发布错误的截图;单击显示“切换到复制粘贴视图”的链接,并将结果文本作为文本发布。@DanielRoseman感谢您提供的提示。在DetailView上发送的电子邮件只是一个测试,这是对联系所有者提供()HostingOfferDetailView
中的函数将不会包含在最后。我已经发布了我的回溯,我确实在{%url%}
标记中传递了关键字args,但我不知道我得到相同错误的原因异常值:contact\u owner\u offer()得到一个意外的关键字参数“email”
@SandeepBalagopal我在我的HostingOfferDetailView
类视图中调用一个基于函数的视图(contact\u owner\u offer()
)。我这样做是因为通过{%url%}调用它可以达到contact\u owner\u offer()
在我的模板中标记,并接收我正在使用的这些参数passing@DanielRoseman我稍微更改了URL正则表达式,指定了
File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /host/lodging-offer/apartacho/
Exception Value: Reverse for 'contact_owner_offer' with arguments '()' and keyword arguments '{'email': 'botibagl@gmail.com', 'interested_email': 'botibagl@gmail.com', 'slug': 'apartacho'}' not found. 1 pattern(s) tried: ['host/contact-to-owner/(?P<lodging_offer_owner_email>[\\w.@+-]+)/from/(?P<interested_email>[\\w.@+-]+)/(?P<slug>[\\w-]+)/$']
url(r'^contact-to-owner/(?P<email1>[\w.@+-]+)/from/' r'(?P<email1>[\w.@+-]+)/(?P<email3>[\w-]+)/$', contact_owner_offer, name='contact_owner_offer'),