1

I am trying to use a default value for url in my views, however, I keep getting a 404 error.

I have set the default value both in my view and in my url path settings:

This is my url:

path('u/<str:username>/<obj>/', views.get_user, kwargs={'obj':None}, name="get_user"),

This is my view:

def get_user(request, username, obj=None):

How can I set the default value of `obj` to none so I can for instance visit `u/jack/posts/` and `u/jack/` using the same URL pattern

1 Answer 1

1

You make two urls, one without the URL parameter and a default, and one with an <obj>:

path('u/<str:username>/', views.get_user, kwargs={'obj':None}, name='get_user'),
path('u/<str:username>/<obj>/', views.get_user, name='get_user'),
Sign up to request clarification or add additional context in comments.

Comments

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.