0

I want SOMETHING to be non mandatory so basically I want www.site.com/endpoint to redirect to www.site.com/something/endpoint automatically. I there a way to do it on one line?

Right now I am doing:

url(r'^SOMETHING/endpoint$', 'endpoint', name='endpoint'),
url(r'^endpoint$', RedirectView.as_view(url='SOMETHING/endpoint')),

Cheers.

3
  • url(r'^(SOMETHING/)?endpoint$', 'endpoint', name='endpoint')? I haven't done django but worth a shot. Commented Sep 27, 2013 at 16:04
  • Can you not just do url(r'^[.*]?endpoint$', 'endpoint', name='endpoint') ? Commented Sep 27, 2013 at 16:08
  • This was my guess as well, but it complains that the view only wants 1 argument but is now given 2. the (SOMETHING/)? seems to become an argument... Commented Sep 27, 2013 at 16:40

2 Answers 2

1

Why do you want to do this on one line? You're talking about two different URLs that do two different things—one is doing an HTTP redirect and the other is rendering a view. Two lines is the right way to go.

Writing a broader regex to cover both URLs will allow you to use the same view for both, but will not cause a redirect (that is, it will not change the URL to SOMETHING/endpoint).

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, this is the feeling I am getting the more I think about it. Thanks for the answer.
1

I believe you are asking to let any url ending in "endpoint" to redirect.

To accomplish this change your redirect url regex to r"endpoint$". The caret operator in regex essentially says from the start of the string.

This regex will match for any url ending in "endpoint", eg. foo/endpoint, bar/endpoint

2 Comments

I see what you are saying but I really want SOMETHING to be in the url as well. Actually I feel like having it only be an endpoint might be an issue since what if i want to have an url like www.site.com/this/is/sparta/you-and-me-endpoint
@Chris Your explanation is very confusing. What is it you are trying to achieve?

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.