NoReverseMatch at Error Django/Python - django

I've tried to explore other questions here about this but having trouble diagnosing my error--hoping someone here can help me out.
Here is the full error:
NoReverseMatch at /
Reverse for 'projects' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Error during template rendering
In template /home/django/chrisblog/posts/templates/posts/home.html, error at line 0
The error happened when I tried to create a few additional pages on the website--just essentially copying the template of another page. These pages are using an extends tag with a 'base.html' template.
My URL file looks like this
from django.conf.urls import url
from django.contrib import admin
import posts.views
import sitepages.views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', posts.views.home, name="home"),
url(r'^resources/', sitepages.views.resources, name="resources"),
url(r'^projects/', sitepages.views.projects, name="projects"),
url(r'^about/', sitepages.views.about, name="about"),
url(r'^posts/(?P<post_id>[0-9]+)/$', posts.views.post_details, name="post_detail"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and my Views file
from django.shortcuts import render
from . import models
def about(request):
return render(request,'sitepages/about.html')
def resources(request):
return render(request,'sitepages/resources.html')
def projects(request):
return render(request,'sitepages/projects.html')
Let me know if this is enough to go on. Thanks for the help.

Related

django 500 error coming instead of 404 for custom error page

here is my views.py
def handler_404(request):
return render(request, '404.html', status=404)
def handler_500(request):
return render(request, '500.html', status=404)
here is my urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from finish.views import handler_404,handler_500
from django.conf.urls import (
handler400,
handler403,
handler404,
handler500
)
urlpatterns = [
path('', include("scrap.urls")),
path('', include("details.urls")),
path('', include("finalize.urls")),
path('', include("finish.urls")),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
handler404 = handler_404
handler500 = handler_500
when i am typing some url in which is not exist
it is coming "Server Error(500)" instead of coming "page not found(404)" error.
Please have a look into this....
Old question, but ill rederect you to Nico's answer. I spent too mutch time finding that.
Im running django 3.0.2 and to sum up i did this:
prodject - urls.py
from django.conf.urls import url, include, handler404
import app.views
urlpatterns = [
.
.
.
]
handler404 = app.views.error_404
app - views.py
.
.
.
def error_404(request, exception):
return render(request,'app/404.html', status = 404)
After reading many tutorials, I found out that most of them ommits the exception
Then put a 404.html in your app/templates/app folder
Hope someone finds this usefull..

Very elementary TypeError in Django

I'm following all of the instructions in this tutorial, time 2:26
https://www.youtube.com/watch?v=gqRLPx4ZeSw&t=3s
and I cannot get the expected result. The TypeError I'm getting is
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
File: urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', 'posts.views.post_home'),
# url(r'^posts/$', '<appname>.views.post_home'),
]
File: views.py
from django.shortcuts import render
from django.http import HttpResponse
def post_home(request):
return HttpResponse("<h1>Hello</h1>")
And here are the relevant screenshots, however I cannot post them because the computer thinks that they're code. Because it thinks they're code when I hit cntrl k the screenshots go away, but if I do not hit cntrl k, then I cannot post the thread.
You should do this for your code to work:
from posts import views as posts_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/$', posts_views.post_home),
]
But it's best you use include to append the app urls, you have to create an
urls.py in your posts app.
from django.conf.urls import url, include
url(r'^posts/', include('posts.urls')),

NoReverseMatch on Django even when kwargs are provided

The Django cant resovle the url, even though the expected kwarg is provided.
Here is root urls.py:
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
url(r'^(?P<domain>\w+)', include('frontend.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here is frontend urls.py:
from django.conf.urls import include,patterns,url
from . import views
from .views import MyprofileView
from .views import SuccessView
from .views import CompanyView
from .views import SubscriptionView
from django.views.decorators.csrf import csrf_exempt
urlpatterns = patterns('',
url(r'/success(/?)$', SuccessView.as_view(), name='success'),
url(r'/subscribe(/?)$', SubscriptionView.as_view(), name='subscribe'),
url(r'^(/?)$', MyprofileView.as_view(), name='home'),
url(r'/api/v1/', include('cpprofile.api.urls')),
url(r'/product', include('product_information.urls')),
url(r'/corporations/(?P<company>\d+)$', CompanyView.as_view(), name='company_page'),
url(r'^/(?P<subscription>\w+)/product/pay/return/(?P<amount>\d+)/(?P<currency>\w+)/(?P<id>\d+)?$',
views.payment_return, name='subscription_product_payment_return'),
)
And here is how I am trying to reverse call it in view.py MyprofileView:
context['subscribe_url'] = redirect('subscribe', kwargs={'domain': 'up'})
What could be wrong here?
Thanks
UPDATE 1
Here is the error I am getting:
django.core.urlresolvers.NoReverseMatch
NoReverseMatch: Reverse for 'subscribe' with arguments '()' and keyword arguments '{'domain': 'up'}' not found. 1 pattern(s) tried: ['(?P<domain>\\w+)/subscribe(/?)$']
You have to unpack the kwargs.
Solution:
kwargs = {'domain': 'up'}
redirect('app_name:subscribe', **kwargs)
EDIT: This will work, no need to change the url.
EDIT2: Prepend app's name and a colon to url name. This finds the url in the app namespace.
Ref: Redirect in Django
I suspect it's because of the (/?). That captures either '' or '/'. So you have to pass that as a non-keyword argument:
redirect('subscribe', '/', domain='up')
So this is in addition to what Sachin Kukreja says.
You need to use reverse to get the correct URL, and then redirect to that.
from django.core.urlresolvers import reverse
return redirect(reverse('subscribe', kwargs={'domain': 'up'}))
In your case, where you seem to be trying to assign the url to a context variable, you shouldn't use redirect at all. Reverse resolves the URL, redirect returns a response.
context['subscribe_url'] = reverse('subscribe', kwargs={'domain': 'up'})
Might also want to follow best practices with your urlconf for consistency, namely end all patterns with '/', but don't start any with '/'. As you do for most of them in the root config:
url(r'^admin/', include(admin.site.urls)), <-- good

NoReverseMatch at /new_application/check_login/

I am new to DJango. I am getting error 'NoReverseMatch at /new_application/check_login/' while redirecting view from another view.
different files listed below.
Main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^new_application/', include('new_application.urls')),
]
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Login
...
def check_login(request):
...
return HttpResponseRedirect(reverse('new_application:loggedin',args=(user,)))
def loggedin(request, user):
return render(request, 'new_application/loggedin.html',{'full_name': user.first_name +" "+ user.last_name})
Application urls.py
from django.conf.urls import url
from . import views
app_name = 'new_application'
urlpatterns = [
url(r'^$', views.login, name='login'),
url(r'^check_login/$', views.check_login, name='check_login'),
url(r'^loggedin/$', views.loggedin, name='loggedin'),
url(r'^invalid_login/$', views.invalid_login, name='invalid_login'),
url(r'^logout/$', views.logout, name='logout'),
]
Here is an error image : error image
please give the solution for fixing this error.
Thank You.
The regex pattern must match against the given url...
url(r'^loggedin/(?P<user>\w+)/$', views.loggedin, name='loggedin'),
Reverse should match one of URL names from urls.py
return HttpResponseRedirect(reverse('loggedin',args=(user,)))

NoReverseMatch error in Django [Pinax]

My Django project based on pinax-social fails loading any page which has {% url home %} in it, and shows this:
NoReverseMatch at /account/login/
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found.
Hardcoding the url fixes the problem, and only the home ReverseMatch fails.
Here's my urls.py:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from .views import *
from NEOreka.models import *
from .forms import SignupForm
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns("neo.views",
url(r"^neo/(?P<neo_id>\d+)/$", "neo_info"),
)
urlpatterns += patterns("",
url(r"^$", "neo.views.home"),
)
urlpatterns += patterns("",
url(r"^admin/", include(admin.site.urls)),
url(r"^account/signup/$", SignupView.as_view(), name="account_signup"),
url(r"^account/", include("account.urls")),
)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Could someone tell me how I could fix this?
Ok I test this one and it works. I hope it will works also in your side.
{% url neo.views.home %}