Django: cannot import name path - python

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

Related

How to redirect urls?

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.

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

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

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: template url tag wrong target

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.

Categories

Resources