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),
]
Related
This is my main urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("shop.urls")),
]
I want that any url entered by user will redirect to shop.urls and find there
like if the user enters /index it will search index in shop.urls not in main urls.
My shop.urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('index', views.index),
]
You need to add / at the end of route so:
urlpatterns = [
path('', views.index),
path('index/', views.index),
]
Then enter the requested url as http://127.0.0.1:8000/index/
In main urls:
just give route name.
urlpatterns = [
path('shop/', include("shop.urls")),
]
And this is your shop urls:
urlpatterns = [
path('index/', views.index),
]
After changing above code.
You can navigate like this in your browser:
localhost:8000/shop/index/
You will redirect to index page.
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"),
]
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.
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
I have in my main urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('scoring.urls', namespace='scoring')),
]
and in my app urls:
urlpatterns = [
url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
url(r'^scoring/$', views.index, name='index'),
]
and in my template:
<li>Scoring</li>
but what {% url 'scoring:index' %}generates is localhost/instead of localhost/scoring. Why?
At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]
Then in your scoring urls.py make sure to add app_name:
app_name = 'scoring'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]
(Note that I removed the scoring prefix in the url patterns.)
Now, as you've added app_name, the reversing in your template should work as expected.