0

I have the LodgingOffer model in which is possible create and lodging offer and detail their data:

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)

To this model, I have one DetailView named HostingOfferDetailView in which I show the data of the LodgingOffer object

One important requisite is that in the detail of the LodgingOffer record, I can contact to owner of the offer (user who create the offer) to apply to her.

For this purpose, I have the contact_owner_offer() function which is the function in where send a email to the owner offer. All this I am doing in the HostingOfferDetailView of this way:

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

My contact_owner_offer function receive these parameters offers and send email to the owner of the offer of the following way:

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)

Until here, all it's works. All is O.K. When I enter to the detail to the offer I send email to owner offer.

My intention is in the template lodging offer detail, render a button which will be useful to contact owner offer, then, I proceed to define my URL which call to the contact_owner_offer() function

I define my url accord to the parameters that receive the contact_owner_offer() function:

This mean, (accord to my understanding - I don't know if I am wrong-) tat my url should receive the email of the lodging offer owner, the email of the user interested in that offer, and the lodging offer title, due to this I create this URL:

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

But when I define this above URL I get this message:

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

In this moment I am a bit confuse about of How to should I define the URL in relation to this scenario, and I also would like know how to should I call this URL in my button in my template detail of the lodging offer.

I am calling of this way, but I don't know if I am in rigth:

<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>

I appreciate some orientation about it.

Best Regards

1
  • 1
    The problem is with the regular expression and having the named group email twice. Could you show an example of the URL you'd expect and if it should contain two email addresses could you not name one something different in the regex? Commented Oct 9, 2017 at 22:28

1 Answer 1

1

You have two groups within the regular expression with the same name. Change the regular expression to name each group in the URL differently.

url(r'^contact-to-owner/(?P<owner_email>[\w.@+-]+)/'
    r'(?P<interested_email>[\w.@+-]+)/(?P<slug>[\w-]+)/$',
    contact_owner_offer, name='contact_owner_offer'),
Sign up to request clarification or add additional context in comments.

5 Comments

it's true, my URL now is well formed. Thanks. My doubt in this moment is How to should I call this URL from a html template button in relation to these parameters which receive? I am trying this {% url 'host:contact_owner_offer' email=lodgingofferdetail.created_by.email email=user_interested_email slug=lodgingofferdetail.slug %} but I get NoReverseMatch
I know that I am using a slug value rather of the lodging_offer_title parameter, which I don't know if this is right ..
Check the parameter names match the URL groups.
Also check the namespace matches.
it is true, there must be a match in the name of the parameters in both the URL definition and the URL call. I take the audacity to ask you if you can see this question that I have asked is related to the call to this same URL that I define here. It's this and I don't understand an inconvenience that arises to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.