NoReverseMatch at / Django - python

I am new to Django and I am trying to create sample project in it. Please help me in resolving this error.
My Main URLs.py file
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^t20_predict/', include('PredictMatch.urls')),
]
My Apps URLs.py file
app_name = 'PredictMatch'
urlpatterns = [
# t20_predict/T20
url(r'^T20/$', views.t20index, name='t20index'),
]

becuase
url(r'^T20/$', views.t20index, name='t20index')
you are not expecting any id ,the url is just t20_predict/T20/ -> see here there is no id
if you want the url to receive id as well the url pattern should be
url(r'^T20/(?P<id>[0-9]+)/$', views.t20index, name='t20index')

Related

Django exclude url pattern

i have a basic django app and want to exclude a route from matching other routes with similar structure
below is my urls.py
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns = [
path('', views.index, name = 'index'),
path('create', views.create, name = 'create'),
path('<str:handle>', views.show, name = 'blogs'),
]
i want path('create'), views.create, name = 'create') to go to the create blog page, and path('<str:handle>', views.show, name = 'blogs'), to go to the details page for a blog.
i get a 404 blog not found error when i navigate to http://localhost:8000/blogs/create
how do i exclude the create from matching the <str:handle> for a blog detail page?
Edit
the url.py for the main app urls
urlpatterns = [
path('blogs/', include(('blogs.urls', 'blogs'), namespace = 'blogs')),
path('admin/', admin.site.urls),
]
Option 1
Did you put your app in installed app in project settings.py.
Option 2.
You need to mention the blogs/urls.py files in project urls.py files.

Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name

I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')
I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?
It must be:
My Hobbies
Without basic_app.
Also you have the same path for index and hobbies . You have to change your url path:
urlpatterns = [
url(r'^$', views.index, name='index'),
...
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
in your template update to
My Hobbies

Django Include() in urls.py with two apps

I believe this is a simple question but I am having a hard time figuring out why this is not working.
I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chart.urls')),
]
and it worked fine.
I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/', include('sales.urls')),
path('', include('chart.urls')),
]
and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/
Raised by: chart.views.index
which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.
I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.
chart.urls:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('dashboard/', views.chart, name='dashboard'),
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
]
sales.urls:
from django.urls import path
from . import views
urlpatterns = [
path('sales/', views.sales, name='Sales Dashboard'),
]
I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.
This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/
Reply to an OLD question but may be useful for others...
from django.urls import path, include
dont for get to import include
Do this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('chart.urls')),
url(r'^sales/', include('sales.urls')),
]
you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),

Having issues with Django URL mapping

I am trying Django for an upcoming project and going through a tutorial and running into issue with defining URL paths. Below is the project structure showing the urls.py at the project root.
The urls.py in my timer package is pretty simple:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
However when I launch the application and navigate to localhost:8080/timer/ I am getting a 404. Any suggestions what I should be looking at?
You can try to change URLs in the settings of the whole project like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^timer/$', include('timer.urls')),
]
As for the timer, the URLs will be like this:
urlpatterns = [
url(r'^$', views.index, name="index"),
]
Don't forget about r's in the url().
It turns out that I created the urls.py under a wrong folder. I misunderstood the instructions and created the urls.py in the same folder as manage.py. Once I add the new url pattern in the file projectdb/projectgb/urls.py, the issue is fixed. Thanks.

how to set the index page in django like http://127.0.0.1:8000/

I have created a small project like this structure..
|-mysite
|-polls
|---static
|-----polls
|-------css
|---------images
|-------images
|-------js
|---templates
|-----polls
|-templates
|---admin
Here In polls is the app and now i am getting output with this url http://127.0.0.1:8000/polls/
In main folder i.e mysite folder in urls.py i have code like this..
urlpatterns = patterns('',
url(r'^polls/',include('polls.urls',namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
Now in polls folder in urls.py code is..
urlpatterns = patterns('',
url(r'^$',views.index, name = 'index'),
}
Now i want to get the page like main website address like.. http://127.0.0.1:8000/
How to get?
Thanks.
Can you change what you have here
url(r'^polls/',include('polls.urls',namespace="polls")),
to
url(r'^', include('polls.urls', namespace="polls")),
Now the reason is this:
Django urls uses regex. In your example, you're telling django to catch your polls app, beginning from the url as localhost:8000/polls/
Learn more: https://docs.djangoproject.com/en/dev/topics/http/urls/
Even in your root project urls.py, you've got this:
# Examples:
# url(r'^$', 'Toolkit.views.home', name='home'), #this will match url of the type localhost:8000
# url(r'^blog/', include('blog.urls')), # this will match url of the type localhost:8000/blog/
Just look sharp!

Categories

Resources