2017-10-09 148 views
0

我有LodgingOffer模型,这是有可能创造和住宿报价和详细说明其数据:传递多个参数,Django的URL

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名为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函数接收这些参数报价和发送电子邮件至以下方式提供的所有者:

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) 

直到这里,所有的工作。全部是O.K. 当我输入要约的细节时,我会向所有者提供发送电子邮件。

我的目的是在模板住宿报价细节,呈现一个按钮,这将是有益的联系店主的报价,然后,我开始定义我的网址,其打电话到contact_owner_offer()功能

我定义我的网址雅阁收到contact_owner_offer()功能的参数:

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

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

但是,当我定义这个上面的网址我得到这个消息:

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>[\[email protected]+-]+)/(?P<email>[\[email protected]+-]+)/(?P<slug>[\w-]+)/$" is not a valid regular expression: redefinition of group name 'email' as group 2; was group 1 at position 43 

在这一刻我有点迷惑有关如何,我应该关系定义网址这种情况下,我也想知道如何在我的住宿优惠的模板细节中的按钮中调用此URL。

我打电话的这种方式,但我不知道我是否在分辩:

<div class="contact"> 
    <a class="contact-button" href="{% url 'host:contact_owner_offer' email=lodgingofferdetail.created_by.email email=user_interested_email slug=lodgingofferdetail.slug %}"> 
    <img src="{% static 'img/icons/contact.svg' %}" alt=""> 
     <span>Contact to owner offer</span> 
    </a> 
</div> 

我很欣赏一些关于它的方向。

最好的问候

+1

问题出在正则表达式上,并且命名组“email”有两次。你能展示一个你期望的URL的例子吗?如果它应该包含两个电子邮件地址,你是否可以在正则表达式中指定一个不同的东西? – AndrewS

回答

1

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

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

这是真的,我的网址现在格式正确。谢谢。我在这个时候的疑问是如何从这个HTML模板按钮中调用这个URL来获取这些参数?我正在尝试这个'{%url'主机:contact_owner_offer'email = lodgingofferdetail.created_by.email email = user_interested_email slug = lodgingofferdetail.slug%}'但是我得到了'NoReverseMatch' – bgarcial

+0

我知道我使用'slug'值'lodging_offer_title'参数,我不知道这是否正确.. – bgarcial

+0

检查参数名称是否与URL组匹配。 – AndrewS