将多个参数传递给Django URL

时间:2017-10-09 21:59:52

标签: python regex django email sendmail

我有LodgingOffer模型,可以在其中创建和提供住宿,并详细说明他们的数据:

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)

    country = CountryField(blank_label='(Seleccionar país)', verbose_name='Pais')

    city = models.CharField(max_length=255, blank = False, verbose_name='Ciudad')

    def __str__(self):
        return "%s" % self.ad_title

    pub_date = models.DateTimeField(
        auto_now_add=True,
    )

    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)

对于此模型,我有一个名为DetailView的{​​{1}},其中我显示了HostingOfferDetailView对象的数据

一个重要的要求是,在LodgingOffer记录的详细信息中,我可以联系要约的所有者(创建要约的用户)以申请她。

为此,我有LodgingOffer功能,这是向所有者提供发送电子邮件的功能。所有这些我都是以contact_owner_offer()这样做的:

HostingOfferDetailView

我的class HostingOfferDetailView(UserProfileDataMixin, LoginRequiredMixin, 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 # LodgingOffer object #lodging_offer_owner = self.get_object() # Get the full name of the lodging offer owner lodging_offer_owner_full_name = self.get_object().created_by.get_long_name() # Get the lodging offer email owner lodging_offer_owner_email = self.get_object().created_by.email # Get the lodging offer title lodging_offer_title = self.get_object().ad_title # Get the user interested email in lodging offer user_interested_email = user.email # Get the user interested full name user_interested_full_name = user.get_long_name() context['user_interested_email'] = user_interested_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 功能会收到这些参数,并通过以下方式向优惠所有者发送电子邮件:

contact_owner_offer

直到这里,一切都有效。一切都是O.K. 当我输入要约的详细信息时,我会向所有者提供电子邮件。

我的目的是在模板住宿优惠详情中,渲染一个对联系所有者提供有用的按钮,然后,我继续定义调用def contact_owner_offer(request, lodging_offer_owner_email, user_interested_email, lodging_offer_title): user = request.user # print(lodging_offer_owner_email, user_interested_email) if user.is_authenticated: # print('Send email') mail_subject = 'Interest in your offer' context = { 'lodging_offer_owner_email': lodging_offer_owner_email, # User offer owner - TO 'offer': lodging_offer_title, # offer title 'user_interested_email': user_interested_email, # user interested in the offer 'domain': settings.SITE_URL, 'request': request } message = render_to_string('contact_user_own_offer.html', context) send_mail(mail_subject, message, settings.DEFAULT_FROM_EMAIL, [lodging_offer_owner_email], fail_silently=True) 功能的URL

我将我的url定义为接收contact_owner_offer()函数的参数:

这意味着,(根据我的理解 - 我不知道我是不是错了) - 我的网址应该会收到住宿优惠所有者的电子邮件,对该优惠感兴趣的用户的电子邮件以及住宿优惠标题,由于这个原因,我创建了这个URL:

contact_owner_offer()

但是当我定义上面的URL时,我收到了这条消息:

url(r'^contact-to-owner/(?P<email>[\w.@+-]+)/'
        r'(?P<email>[\w.@+-]+)/(?P<slug>[\w-]+)/$',
        contact_owner_offer, name='contact_owner_offer'),

在这一刻我有点混淆如何定义与此场景相关的URL,我也想知道如何在我的按钮中调用我的模板详细信息中的这个URL

我这样称呼,但我不知道我是否处于严格状态:

File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/sre_parse.py", line 759, in _parse
    raise source.error(err.msg, len(name) + 1) from None
sre_constants.error: redefinition of group name 'email' as group 2; was group 1 at position 43

django.core.exceptions.ImproperlyConfigured: "^contact-to-owner/(?P<email>[\w.@+-]+)/(?P<email>[\w.@+-]+)/(?P<slug>[\w-]+)/$" is not a valid regular expression: redefinition of group name 'email' as group 2; was group 1 at position 43

我很欣赏它的一些方向。

最好的问候

1 个答案:

答案 0 :(得分:1)

正则表达式中有两个组具有相同的名称。更改正则表达式以不同方式命名URL中的每个组。

url(r'^contact-to-owner/(?P<owner_email>[\w.@+-]+)/'
    r'(?P<interested_email>[\w.@+-]+)/(?P<slug>[\w-]+)/$',
    contact_owner_offer, name='contact_owner_offer'),