Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib import admin
- from django.urls import path, include
- from django.conf.urls.static import static
- from django.conf import settings
- from users.views import login_view, register_view, logout_view
- # from django.contrib.auth import views as auth_views
- # from users import views as user_views
- from posts.views import index,postDetail, categoryDetail, blog, search
- urlpatterns = [
- path('admin/', admin.site.urls),
- path('', index, name="home"),
- path('blog/', blog, name="blog"),
- path('search/', search, name='search'),
- path('<slug>/', postDetail, name='post-detail'),
- path('category/<slug>/', categoryDetail, name='category-detail'),
- path('login/', login_view),
- path('logout/', logout_view),
- path('register/', register_view),
- # path('register/', user_views.register, name='register'),
- # path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
- # path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
- path('tinymce/', include('tinymce.urls'))
- ]
- LOGIN_REDIRECT_URL = 'home'
- LOGIN_URL = 'login'
- from django import forms
- from django.contrib.auth import (
- authenticate,
- get_user_model
- )
- User = get_user_model()
- class UserLoginForm(forms.Form):
- username = forms.CharField()
- password = forms.CharField(widget=forms.PasswordInput)
- def clean(self, *args, **kwargs):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- if username and password:
- user = authenticate(username = username, password=password)
- if not user:
- raise forms.ValidationError('This user does not exits')
- if not user.check_password(password):
- raise forms.ValidationError('Incorrect Password')
- if not user.is_active:
- raise forms.ValidationError('This user is not active')
- return super(UserLoginForm, self).clean(*args, **kwargs)
- class UserRegisterForm(forms.ModelForm):
- email = forms.EmailField(label='Email Address')
- email2 = forms.EmailField(label= 'Confirm Email')
- password = forms.CharField(widget=forms.PasswordInput)
- class Meta:
- model = User
- fields =[
- 'username',
- 'email',
- 'email2',
- 'password'
- ]
- def clean(self, *args, **kwargs):
- email = self.cleaned_data.get('email')
- email2 = self.cleaned_data.get('email2')
- if email != email2:
- raise forms.ValidationError('email must match')
- eamil_qs = User.objects.filter(email=email)
- if eamil_qs.exists():
- raise forms.ValidationError(
- "This email is already being used"
- )
- return super(UserRegisterForm, self).clean(*args, **kwargs)
- # Create your views here.
- from django.shortcuts import render, redirect
- from django.contrib.auth import (
- authenticate,
- get_user_model,
- login,
- logout
- )
- from .forms import UserLoginForm, UserRegisterForm
- # Create your views here.
- def login_view(request):
- next = request.GET.get('next')
- form = UserLoginForm(request.POST or None)
- if form.is_valid():
- username = form.cleaned_data.get('username')
- password = form.cleaned_data.get('password')
- user = authenticate(username= username, password=password)
- login(request, user)
- if next:
- return redirect(next)
- return redirect('/')
- context = {
- 'form': form
- }
- return render(request, 'login.html', context)
- def register_view(request):
- next = request.GET.get('next')
- form = UserRegisterForm(request.POST or None)
- if form.is_valid():
- user = form.save(commit=False)
- password = form.cleaned_data.get('password')
- user.set_password(password)
- user.save()
- new_user = authenticate(username= user.username, password=password)
- login(request, new_user)
- if next:
- return redirect(next)
- return redirect('/')
- context = {
- 'form': form
- }
- return render(request, 'signup.html', context)
- def logout_view(request):
- logout(request)
- return redirect('/login')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement