0

I have seen similar questions, this is not a duplicate. I have a simple redirect that recieves kwargs that is not working and I don't know why. The docs give this example:

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

And it is exactly what I'm doing:

...
...
return redirect('perfiles:despues_contacto', razon_contacto='donacion', correo_contacto='thrall@gmail.com')

The url pattern looks like this:

url(r'^gracias_por_contactarnos/(?P<razon_contacto>[-\w]+)/(?P<correo_contacto>[-\w]+)/$',
        views.DespuesContacto.as_view(), name="despues_contacto"),

?¿ What am I missing? maybe it is something obvious ... but my eyes are tired of trying to find the error. Is it the regex?

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

2 Answers2

1

Ok so the regex was wrong, it didn't accept "@" or "." ... and because it is really difficult to use a regex for emails, as described in this SO question. I processed the email, then send it to the url, and manually built it again:

email_for_url = email.split(".")[0].replace("@", "-")    
return redirect('perfiles:despues_contacto', razon_contacto=razon_contacto, correo_contacto=email_for_url)

...
def get_context_data(self, **kwargs):
   ....
    email = kwargs["email"].replace("-", "@") + ".com"
Community
  • 1
  • 1
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
  • 1
    What if original email already has `"-"` before `"@"`? It will rebuild wrong email. If you have to do it like this, use more complex identifier for `"@"` i.e. `"--__--"`. – primoz Apr 06 '16 at 07:04
1

.Very simple. You only change regular expression of url to it undestand parameter have @ character.

urls.py

    url(r'^gracias_por_contactarnos/(?P<razon_contacto>[-\w]+)/(?P<correo_contacto>[\w\-@.]+)/$',
    views.DespuesContacto.as_view(), name='despues_contacto'),
Son Lam
  • 1,229
  • 11
  • 16