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

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

Related

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.

How to retain the development page

Once I started populating the "urlpatterns" list in urls.py. I am no longer able to see the development page.
How can I retain it?
Edit: By default it is like
urlpatterns = [
path('admin/', admin.site.urls),
]
I have changed to:
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Also, "development page" I meant the default (initial page), like below.
if you really need it, you can add it manually with path. For example:
from django.views.debug import default_urlconf
urlpatterns = [
path('admin/', admin.site.urls),
path('start/', default_urlconf),
]

Django: cannot import name path

My urls.py looks like this:
urlpatterns = [
path('',views.index, name='index'),
path('entry/(<int:pk>)' , views.details,name='details'),
path('admin/', admin.site.urls),
]
but when i try to run it i get error as cannot find path.
Attempt 1 :
I tried to use url instead but I am not sure how to use second line into url. This does not seems to work:
urlpatterns = [
url(r'^$',views.index, name='index'),
url(r'^entry/(?P<pk>\d+)/' , views.details,name='details'),
url(r'^admin/', admin.site.urls),
]
I wanted to comment on Monhammand's answer. But, I couldn't do that, because I needed to have at least 50 reputations. So, I submit this as an answer.
If you would like to use regular expressions in Django 2.X, you can use re_path().
https://docs.djangoproject.com/en/2.0/ref/urls/#re-path
urlpatterns = [
re_path(r'^$',views.index, name='index'),
re_path(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
re_path(r'^admin/', admin.site.urls),
]
If you are using django 2.x, do like this:
urlpatterns = [
path('',views.index, name='index'),
path('entry/<int:pk>/' , views.details,name='details'),
path('admin/', admin.site.urls),
]
If you are using django 1.x, do like this:
urlpatterns = [
url(r'^$',views.index, name='index'),
url(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
url(r'^admin/', admin.site.urls),
]
/ and $ are important

When adding urlpatterns in Django, why is views.index called instead of views.index()?

I was wondering, in the url.py-files, why are parentheses not used when calling methods from the view.py-files?
For example, why does the following code not have a parenthesis at the end of views.index?
urlpatterns = [
url(r'^$', views.index, name='index'),
]

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

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

Categories

Resources