django, how to mapping no other address, but just domain? - python

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')
]

Related

Django urls py. Loading the index page instead of the image

I currently have two situations
A) http://127.0.0.1:8000/
B) http://127.0.0.1:8000/Images/SomeImages.png
This is what my urls.py looks like
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include("webSite.urls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and then my webSite.urls looks like this
urlpatterns = [
url(r"", test , name="test"),
]
The problem with this is it works for condition A but for condition B it routes to the main page as well instead of the image. Can anyone tell me how I can fix this?
You should include the ^ and $ anchors to mark the start and end of the string:
urlpatterns = [
url(r'^$', test , name="test"),
]
or work with a path(…) which will compile a regex with these anchors:
urlpatterns = [
path('', test , name="test"),
]

Why am I seeing localhost:8000 empty path does not match Django?

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') ),
]

Django : Cannot resolve the URLs pattern : Always Index page content is displayed

I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.

Django Interchanging the urls by overiding regex part

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'),
]

Match all urls without the prefix in Django

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.

Categories

Resources