2017-10-09 203 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