No Reverse Match when calling url from html file - Django - python

So I am trying to call my url by using the following in one of my html templates -
<a href="{% url 'socialx:index' %}">
My apps urls.py file looks like this -
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^socialx/', include('socialx.urls')),
url(r'^admin/', include(admin.site.urls)),
]
And the root urls.py file is like this -
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^socialx/', include('socialx.urls'), name='socialx'),
url(r'^admin/', include(admin.site.urls)),
]
When navigating to the app via browser I get the following error -
NoReverseMatch at /socialx/ 'socialx' is not a registered namespace

Have a look at the documentation
I think that when you include the url in urls.py you need to add a namespace
url(r'^socialx/', include('socialx.urls', namespace="socialx")),
And make sure that the url for going to the index page has a name=index.
See if that helps.

Related

Django - The current path, job/all, matched the last one. - But it still returns 404

When I go to this URL http://127.0.0.1:8000/job/all I am always receiving a 404 error with this message:
404 Django Error screenshot
However, when I just go to http://127.0.0.1:8000/ or http://127.0.0.1:8000 it always finds the / (index) route correctly regardless if there is a trailing slash, or not. This issue seems to only happen with the /job/WHATEVER_ELSE_GOES_HERE URLs.
I have the following URLs setup:
My jobs app urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name="index"),
path('job/<int:job_id>', job_details, name="job_detail"),
path('job/all/', all_jobs, name="all_jobs"),
path('job/add/', add_job, name="add_job"),
path('job/my_posted_jobs/', my_posted_jobs, name="my_posted_jobs"),
path('job/delete/<int:job_id>', delete_job, name="delete_job"),
path('job/search', search_job, name="search_job"),
path('job/apply_for_job/<int:job_id>', apply_for_job, name="apply_for_job")
]
My project's urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('jobs.urls')),
path('user/', include('users.urls')),
path('payments/', include('payments.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In the error message (screenshot) it also says this:
The current path, job/all, matched the last one.
How come it matches a URL but returns 404?
How do I fix it?
Thank you
You have to include the "/" since you have included it in your syntax in your urls.py therefore call the url from your web browser using the link below when you have started the Django web server.
http://127.0.0.1:8000/job/all/

can not import patterns in django 1.11.4

My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.

Django: Page Not Found

I'm getting a 404 from Django and none of the previous posts on the subject (of which there are many) seem to have helped.
views.py
from django.views.generic.detail import DetailView, SingleObjectMixin
from app.models import MyModel
class MyDetails(DetailView, SingleObjectMixin):
template_name = "app/my_view.html"
model = MyModel
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import MainList, post_form_upload, MyDetails
urlpatterns = [
url(r'^$', MainList.as_view(), name="main_list"),
url(r'^add_something$', post_form_upload, name="add_something"),
url(r'^my_details/(?P<pk>\d+)$', MyDetails.as_view(), name="my_details"),
]
app/urls.py
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
(r'^$', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
when I enter the URL: http://localhost:8000/my_details, I get the following error:
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='main_list']
^add_something$ [name='add_something']
^my_details/(?P<pk>\d+)$ [name='my_details']
The current URL, my_details, didn't match any of these.
The other two URLs (/ and /add_something) work fine.
First of all, not sure how you are not running into this issue, but in your app/urls.py
(r'^$', include('app.urls')),
should be
(r'^/', include('app.urls')),
$ indicates the end of regex pattern, and anything inside include() would not be included.
Secondly, None of your URL patterns match my_details/ They only match my_details/<id>
A little more on the documentation of URL regex, etc..

django urlconf 404 on webroot

Django: 1.8.5
Python: 3.5
I'm getting a 404 on just the webroot. I don't know what's wrong with my urlconf.
Directory structure:
myproject // from django-admin startproject
myproject // '' ''
__init__.py
settings.py
urls.py
views.py
myapp // from manage.py startapp
migrations
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
In myproject > urls.py:
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^/$', include(app_urls)),
)
And in myapp > urls.py:
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^/$', 'index'),
)
I run the django development server and I get a 404 on the index page, even though I have the empty regex defined.
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^/$
The current URL, , didn't match any of these.
The documentation in urls.conf from a a fresh install of django:
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
Changing the regex in BOTH urls.py files to r'^' appears to work, but with a new 'Could not import 'index'. error.
Don't use $, when using include. Change it to:
# myproject/urls.py
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^', include(app_urls)),
)
# myapp/urls.py
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^$', 'index'),
)
The root URL is ^$, not ^/$.
Also, when you include a urlconf from another urlconf, it uses both patterns together; it doesn't forget the original pattern. So your app is really looking for the url ^/$^/$, which I'm not sure can ever be matched.

Django urlpattern "didn't match"

I have the following code in my urls.py:
urlpatterns = patterns('',
(r'^news/', include('news.urls')),
)
When I try to open
http://localhost/news
or
http://localhost/news/
in the browser django shows me 404 page:
Using the URLconf defined in python.urls, Django tried these URL patterns, in this order:
^news/
The current URL, , didn't match any of these.
UPD:
news/urls.py:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('news.views',
(r'^$', 'news'),
)
There is views.py in news directory and it contains news function.
And news module is added to INSTALLED_APPS.
Why it cannot find news pattern? Any suggestions?
Looks like you forgot the url() function:
Try this instead:
from django.conf.urls import url
urlpatterns = patterns('',
url(r'^news/', include('news.urls')),
)
and news/urls.py:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.conf.urls import url
urlpatterns = patterns('news.views',
url(r'^$', 'news'),
)
In which port your application launches? When I see your tries http://locahost/news it might be that you forgot to use the right port.
Try to visit http://localhost:8000/ and http://localhost:8000/news.
I think it may be a problem with your views.py this url on the django docs tells you how to dispatch the urls properley
https://docs.djangoproject.com/en/dev/topics/http/urls/

Categories

Resources