When requesting[GET] 127.0.0.1:8000/restaurant/1 i get a clean json and 200 status code
urlpatterns = [
url(r'^restaurant',views.Restaurant_List_Create.as_view(), name='all_restaurants'),
url(r'^restaurant/(?P<pk>\d+)',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),
]
but when i interchange the url codes it runs the views.Restaurant_List_Create.as_view() (overrides the regex url)
urlpatterns = [
url(r'^restaurant/(?P<pk>\d+)',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),
url(r'^restaurant',views.Restaurant_List_Create.as_view(), name='all_restaurants'),
]
You url matches both because you don't have included $ sign at the end of your urls.
You can change them as follow :
urlpatterns = [
url(r'^restaurant/(?P<pk>\d+)$',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),
url(r'^restaurant$',views.Restaurant_List_Create.as_view(), name='all_restaurants'),
]
Related
I keep running into 'Page not found' when trying to load http://localhost:8000/ . I have an empty path set to direct the page to my project_index page, but this is all I get:
I have checked and re-checked all of my urlpatters, but cannot figure out where the problem is.
Here are all my urls.py scripts that correspond to this project:
personal_portfolio/:
projects/:
blog/:
You don't have anything starting with
urlpatterns = [
path("", ..... ),
]
in your main urls.py.
like all the URLs you defined has a suffix thus Django can't find it,
either you need to do
urlpatterns = [
path("", include('project.urls') ),
]
or
urlpatterns = [
path("", include('blog.urls') ),
]
my url pattern in apps like:
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
the url : /show/in_arg1/in_arg2%2F_this_is_arg2 match to view_3
but I want to let the url match view_2
I try to change urlpatterns like
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.\/]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.\/]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
the url : /show/in_arg1/in_arg2%2F_this_is_arg2 works well
but the url /show/in_arg1/in_arg2/in_arg3 will match to view_2, not what I want
It seems django decode %2F to / before url matching
Can I let django do url matching without decode %2F ?
Or some way to solve my problem
thanks
It's the \/in (?P<arg2>[\w\d_\.\/]+) that's messing you up. They would even match show/a//b!
Try these:
urlpatterns = [
url(r'^show/(?P<arg1>[\w\d_\.]+)$', views.view_1),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)$', views.view_2),
url(r'^show/(?P<arg1>[\w\d_\.]+)/(?P<arg2>[\w\d_\.]+)/(?P<arg3>[\w\d_\.]+)$', views.view_3),
]
Somebody more savvy in regex might help you further.
myproject/urls.py
urlpatterns = [
url(r'^$', include('website-app.urls', namespace='website')),
]
website-app/urls.py
urlpatterns = [
url(r'^$', views.somedef, name='somename')
]
I want to connect url domain.com/ to views.somedef which is not in myproject.
What domain.com/ means is just domain and end of address with /.
It is with No other parameters.
domain.com/other/parameters/ has other parameters (other/parameters/), so it is not what I want.
If I run django with that above code, django says
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
Is there any good way to use url domain.com/ in website-app/urls.py ,
Not in myproject/urls.py?
This should work
myproject/urls.py
urlpatterns = [
url(r'^', include('website-app.urls', namespace='website')),
]
website-app/urls.py
urlpatterns = [
url(r'$', views.somedef, name='somename')
]
I want my link detail url like this: /subreddit/pk/slug
Subreddit(category) is variable of course.
In Reddit if you click the comments of the link, you are going to detail of it. But detail page is mix of subreddit. How can I do that?
I hope I made myself clear. Thanks in advance.
I'm putting here some URLs code example:
base/urls.py
urlpatterns = [
url(r'^', include("x.links.urls", namespace="link")),
url(r'^k/', include("x.subreddit.urls", namespace="sub")),
]
link/urls.py
urlpatterns = [
url(
regex=r'^(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$',
view=views.LinkDetailView.as_view(),
name='link_detail'
),
]
subreddit/urls.py
urlpatterns = [
url(
regex=r'^(?P<slug>[\w-]+)/$',
view=views.SubredditDetailView.as_view(),
name='subreddit_detail'
),
]
you have to change the order of URL include. It take priority as first come first server.In you case it running the links URL,because there is match to your URL pattern. If you declare like below.
urlpatterns = [
url(r'^k/', include("x.subreddit.urls", namespace="sub")),
url(r'^', include("x.links.urls", namespace="link")),
]
It will first match k then go to link urls.
In link/urls.py try to add regex that parses arguments of your desired url:
urlpatterns = [
url(
regex=r'^(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$',
view=views.LinkDetailView.as_view(),
name='link_detail'
),
url(
regex=r'^(?P<subreddit>[\w-]+)/(?P<pk>\d+)/(?P<slug>[\w\d-]+)/$',
view=views.SubredditDetailView.as_view(),
name='subreddit_detail'
),
]
The second URL-pattern will pass arguments subreddit, pk and slug to the subreddit view function.
I'm currently using the following urls.py:
api_patterns = [
url(r'^users/', include('users.urls', namespace='user')),
]
internal_patterns = [
# ...
]
urlpatterns = [
url(r'^api/', include(api_patterns)),
url(r'^internal/', include(internal_patterns)),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?!(?:api|internal|admin)/)', MainView.as_view()),
]
The point of this config is to render MainView if url doesn't have the api, internal or admin prefix:
/api/users/... — found
/api/foo/ — not found
/foo/ — found
How can I make it simplier and more intent revealing?
I think your intent will be more clear if you do this in two urls:
url(r'^(api|internal|admin)/', SomeView.as_view()),
url(r'^.*', MainView.as_view())
MainView will be executed only if a url does not begin with api, internal or admin.
SomeView will be executed if a url begins with api/internal/admin but doesn't match the patterns above it. You can customize this view to either return a default 404 page, or perform other functions as you need.
Using your examples:
/api/users will execute include(api_patterns)
/api/foo will execute SomeView
/foo will execute MainView
Edit
To address the first point in your comment: url patterns are regexes, while you can extract these into variables to remove duplication, it can make code hard to read. Here's one example for removing duplication (I'm sure you can come up with others):
d = OrderedDict([
(r'api', api_patterns),
(r'internal', internal_patterns),
(r'admin', admin.site.urls),
])
main_view_re = r'^!({})/'.format('|'.join(d.keys()))
urlpatterns = [url(r'^{}/'.format(k), include(v)) for k, v in d]
urlpatterns.append(url(main_view_re, MainView.as_view()))
For django >= 3 rather use re_path:
from django.urls import re_path
urlpatterns = [
re_path(r'^.*',MainView.as_view())
]
urlpatterns = [
url(r'^api/', include(api_patterns)),
url(r'^internal/', include(internal_patterns)),
url(r'^admin/', include(admin.site.urls)),
url(r'', MainView.as_view()),
]
Leaving no prefix would allow you to catch any URL that a user might try after the URL conf matches the api, internal and admin url's.