how to customize the 403 error page in django? - python

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView,ListView
from .models import Article
from django.urls import reverse_lazy
class ArticleListView(LoginRequiredMixin,ListView):
model = Article
template_name = 'article_list.html'
login_url = 'login'
How can i customize the 404 error page?

Many resources are decorated error pages if there is a failure in processing a request from the client.
Use this link https://evileg.com/en/post/9/

Related

AttributeError at /api/test type object 'Product' has no attribute 'objects'

So I get AttributeError at /api/test
type object 'Product' has no attribute 'objects' error when I try to load up the page via get request, I'm trying to make an API using django rest framework and it says Product has no objects attribute.
views.py
from django.shortcuts import render
from rest_framework import viewsets
from .serializers import ProductSerializer
from .models import Product
from rest_framework.views import APIView
from rest_framework.response import Response
class Product(APIView):
def get(self, request, format=None):
products = Product.objects.all()
serializer = ProductSerializer(products)
return Response(serializer)
urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from . import views
# router = routers.DefaultRouter()
# router.register('products', views.ProductViewSet)
# router.register('test', views.Product)
# urlpatterns = router.urls
urlpatterns = [
path('test', views.Product.as_view())
]
serializers.py
class ProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Product
fields = ['name','price','description']
change the name of view, ProductView because its overriding Product model thats why you are getting this error

NameError: name 'PostCreateView' is not defined

I'm using Django 3.0.8 I'm getting an error in path('post/new/',PostCreateView.as_view(),name='post-create') in blog/urls.py(blog is an app under my django_project). I've a class named PostCreateView in blog/views.py but it throws NameError:name 'PostCreateView' is not defined.
My blog/views.py is here:
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import Post
# Create your views here.
def home(request):
context={
'posts':Post.objects.all()
}
return render(request,'blog\home.html',context)
class PostListView(ListView):
model = Post
template_name='blog/home.html' # <app>/<model>_<viewtype>.html
context_object_name='posts'
ordering=['-date_posted']
class PostDetailView(DetailView):
model = Post
class PostCreatelView(CreateView):
model = Post
fields=['title','content']
def about(request):
return render(request,'blog\about.html',{'title':'About'})
and blog/urls.py is here:
from django.urls import path
from .views import PostListView, PostDetailView, PostCreatelView
from . import views
urlpatterns = [
path('',PostListView.as_view(),name='blog-home'),
path('post/<int:pk>',PostDetailView.as_view(),name='post-detail'),
path('post/new/',PostCreateView.as_view(),name='post-create'),
path('about/',views.about,name='blog-about'),
]
What is wrong with this code?
Thanks in advance.
You have declared PostCreatelView (with an "l") not PostCreateView.

Not able to link a static web page using reverse_lazy

I have written some code to develop a social media site. In that, I wanted to redirect to homepage look-a-like which i have created. I tried to create a url for this page, but there is no use. I am getting the error like:
NoReverseMatch at /accounts/signup/
Reverse for 'start' not found. 'start' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/signup/
Django Version: 1.11.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'start' not found. 'start' is not a valid view function or pattern name.
In this project, there are three apps. Among them, "accounts" is one app. In accounts, I have created the templates folder with three files.("login.html,signup.html and start.html"). The views.py file for this :
from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView
from . import forms
from django.views.generic import TemplateView
class MyView(TemplateView):
template_name = 'accounts/start.html'
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("start")
template_name = "accounts/signup.html"
And the urls.py file is:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r"login/$", auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
url(r"logout/$", auth_views.LogoutView.as_view(), name="logout"),
url(r"signup/$", views.SignUp.as_view(), name="signup"),
url(r"start/$", views.MyView.as_view(), name="start")
]
How to redirect to "start.html" page from the signup page. I have created start view but still, it is showing error as above. Please help me with this.
url(r'^start/$', views.MyView.as_view(), name="start")
reverse_lazy("accounts:start")
^ for pattern start,$ for pattern end;You use namespaced url,see Reversing namespaced URLs;

Linking outer html page to the reverse_lazy() of signup page

I have written code to develop a social media clone site using django. In that, after signup, i have to move to homepage instead of login page. The homepage html code is present in another folder outside of this current folder. I am unable to access the url of the html page from this. The code is:
from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView
from . import forms
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "accounts/signup.html"
Instead of "login", the url should be "base". The path of "login.html" is: "project/accounts/templates/accounts/login.html". And the path of "base.html" is: "project/templates/project/base.html". Please suggest me a method to modify this.
You need to create the according URL and view, so you can redirect to that view:
# views.py
from django.views.generic import TemplateView
class MyView(TemplateView):
template_name = 'project/base.html'
# urls.py
from django.conf.urls import url
from .views import MyView
urlpatterns += url(r'^my_url/', MyView.as_view(), name='my_view')
# signup view
from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView
from . import forms
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("my_view")
template_name = "accounts/signup.html"
You may need to adapt a couple things, but the key is, that you need a URL and a View so you can redirect them there, once they signed up.
You should use it with your application name.
url:
path('email-listesi/kayit-silindi/', UnsubscriptionSuccessful.as_view(), name='unsubscription-successful'),
views:
success_url = reverse_lazy('yourapplicationname:unsubscription-successful')

error "The current path, user_info/, didn't match any of these."

Well, this question has been asked by many users, and I have tried some of the resolution provided in the query, but that doesn't seem to resolve my issue, maybe I am doing some other mistake.
I am trying to create a simple user registration/login/logout forms
SchoolnSkill ---Project name, and below is the SchoolnSkill/url details:-
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^user_info/', include('user_info.urls')),
]
user_info--App name, and the user_info/url details are below:-
from django.conf.urls import url
from . import views
urlpatterns = [
# url(r'^$', views.index, name='index'),
url(r'^registration/', views.UserFormView, name='registration')
]
My views.py output is as below:-
from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import authenticate, login
from django.views import generic
from .models import Registration
from django.views import View
from .forms import UserForm
class UserFormView(View):
#from_class = UserForm
template_name = 'user_info/registration_form.html'
#dispaly blank form, new user coming to the website
def get(self, request):
form = self.UserForm(None)
return render(request, self.template_name, {'form':form})
def post(self, request):
form = self.UserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
form.py content as below:
from django.contrib.auth.models import User
from django import forms
from django.forms import ModelForm
class UserForm(forms.ModelForm):
password = forms.CharField(widget =forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
I am using below versions:
Python 3.6
Django 1.11.3
Spyder IDE
I am pretty sure that am doing something silly.
Please help.
below is the error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/user_info/
Using the URLconf defined in SchoolnSkill.urls, Django tried these URL patterns, in this order:
^admin/
^user_info/ ^registration/ [name='registration']
The current path, user_info/, didn't match any of these.
as you are using class based view you need to add as_view in the url
so
change this in user_info url to
urlpatterns = [
url(r'^registration/', views.UserFormView.as_view(), name='registration')
]

Categories

Resources