SHARE
TWEET

Untitled

a guest Apr 2nd, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.shortcuts import render
  2. from basic_app.forms import UserForm,UserProfileInfoForm
  3.  
  4. from django.urls import reverse
  5. from django.contrib.auth.decorators import login_required
  6. from django.http import HttpResponseRedirect,HttpResponse
  7.  
  8.  
  9. from django.contrib.auth import authenticate,login,logout
  10.  
  11. # Create your views here.
  12.  
  13. def index(request):
  14.     return render(request,'basic_app/index.html')
  15.  
  16. @login_required
  17. def user_logout(request):
  18.     logout(request)
  19.     return HttpResponseRedirect(reverse('index'))
  20. @login_required
  21. def special(request):
  22.     return HttpRespnse("You are logged in")
  23.  
  24. def register(request):
  25.  
  26.     registered=False
  27.  
  28.     if request.method=="POST":
  29.         user_form=UserForm(data=request.POST)
  30.         profile_form=UserProfileInfoForm(data=request.POST)
  31.  
  32.         if user_form.is_valid() and profile_form.is_valid():
  33.  
  34.             user=user_form.save()
  35.  
  36.             user.set_password(user.password)
  37.             user.save()
  38.  
  39.             profile=profile_form.save(commit=False)
  40.             profile.user=user
  41.  
  42.             if 'profile_pic' in request.FILES:
  43.                 profile.profile_pic=request.FILES['profile_pic']
  44.  
  45.             profile.save()
  46.  
  47.             registered=True
  48.         else:
  49.             print(user_form.errors,profile_form.errors)
  50.     else:
  51.         user_form=UserForm()
  52.         profile_form=UserProfileInfoForm()
  53.     return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered})
  54.  
  55.  
  56.  
  57.  
  58.  
  59. def user_login(request):
  60.  
  61.  
  62.     if request.method=='POST':
  63.         username=request.POST.get('username')
  64.         password=request.POST.get('password')
  65.  
  66.         user1=authenticate(username=username,password=password)
  67.  
  68.         if user1:
  69.             if user1.is_active:
  70.                 login(request,user1)
  71.                 request.session.set_expiry(6000)
  72.                 return HttpResponseRedirect(reverse('index'))
  73.             else:
  74.                 return HttpResponse("Account not active")
  75.         else:
  76.             print("Someone tried to login and failed")
  77.             return HttpResponse("invalid login details")
  78.     else:
  79.         return render(request,'basic_app/login.html',{})
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. ==============================================
  88.  
  89.  
  90. from django.conf.urls import include,url
  91. from basic_app import views
  92.  
  93. app_name='basic_app'
  94.  
  95. urlpatterns=[
  96.     url(r'^register/$',views.register,name='register'),
  97.     url(r'^user_login/$',views.user_login,name='user_login')
  98. ]
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. """learning_users URL Configuration
  106.  
  107. The `urlpatterns` list routes URLs to views. For more information please see:
  108.     https://docs.djangoproject.com/en/2.1/topics/http/urls/
  109. Examples:
  110. Function views
  111.     1. Add an import:  from my_app import views
  112.     2. Add a URL to urlpatterns:  path('', views.home, name='home')
  113. Class-based views
  114.     1. Add an import:  from other_app.views import Home
  115.     2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
  116. Including another URLconf
  117.     1. Import the include() function: from django.urls import include, path
  118.     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
  119. """
  120. from django.contrib import admin
  121. from django.urls import path
  122. from django.conf.urls import url,include
  123. from basic_app import views
  124.  
  125. urlpatterns = [
  126.     url(r'^$',views.index,name='index'),
  127.     url(r'^basic_app/',include('basic_app.urls')),
  128.     path('admin/', admin.site.urls),
  129.     url(r'^logout/$',views.user_logout,name='logout'),
  130.     url(r'special/',views.special,name='special'),
  131. ]
  132.  
  133.  
  134.  
  135.  
  136. from django import forms
  137. from django.contrib.auth.models import User
  138. from basic_app.models import UserProfileInfo
  139. class UserForm(forms.ModelForm):
  140.     password=forms.CharField(widget=forms.PasswordInput())
  141.  
  142.     class Meta():
  143.         model=User
  144.         fields=('username','email','password')
  145.  
  146. class UserProfileInfoForm(forms.ModelForm):
  147.     class Meta():
  148.         model=UserProfileInfo
  149.         fields=('portfolio_site','profile_pic')
  150.  
  151.  
  152.  
  153.  
  154.  
  155. from django.db import models
  156.  
  157. from django.contrib.auth.models import User
  158.  
  159. # Create your models here.
  160. class UserProfileInfo(models.Model):
  161.  
  162.     user=models.OneToOneField(User,on_delete=models.CASCADE)
  163.  
  164.     portfolio_site=models.URLField(blank=True)
  165.  
  166.     profile_pic=models.ImageField(upload_to='profile_pics',blank=True)
  167.  
  168.     def __str__(self):
  169.         return self.user.username
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top