-
Notifications
You must be signed in to change notification settings - Fork 15
Assignment-2 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Assignment-2 #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from django.contrib import admin | ||
| from .models import User | ||
| # Register your models here. | ||
| admin.site.register(User) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class AuthenticationConfig(AppConfig): | ||
| name = 'authentication' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from django.contrib.auth.forms import UserCreationForm | ||
| from django.contrib.auth.models import User | ||
|
|
||
|
|
||
| class CreateUserForm(UserCreationForm): | ||
| class Meta: | ||
| model = User | ||
| fields = ['email', 'password'] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Generated by Django 3.1.2 on 2021-06-20 12:48 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='User', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('email', models.CharField(max_length=100, unique=True)), | ||
| ('password', models.CharField(max_length=100)), | ||
| ], | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from django.db import models | ||
|
|
||
| # Create your models here. | ||
| class User(models.Model): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not good, User is a pre-defined class, you should name this something else and not User. |
||
| email = models.CharField(max_length=100,unique=True) | ||
| password = models.CharField(max_length=100) | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"{self.email}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | ||
|
|
||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <h2 class="text-center pt-4"> Hello, welcome back</h2> | ||
|
|
||
| <!-- <form method="POST" > | ||
| {% csrf_token %} | ||
| <input type="email" name="email" placeholder="enter your email"> | ||
| <input type="password" name="password" placeholder="enter your password"> | ||
| <input type="submit"> | ||
| </form> --> | ||
| <main class="d-flex align-items-center min-vh-100 py-3 py-md-0"> | ||
| <div class="container"> | ||
| <div class="card login-card"> | ||
| <div class="row no-gutters"> | ||
| <div class="col-md-5"> | ||
| <img src="https://source.unsplash.com/tMI2_-r5Nfo/400x400" alt="login" class="login-card-img"> | ||
|
|
||
| </div> | ||
| <div class="col-md-7"> | ||
| <div class="card-body"> | ||
|
|
||
| <b> <p class="login-card-description text-center">Sign into your account</p> </b> | ||
| <form method="POST"> | ||
| {% csrf_token %} | ||
| <div class="form-group"> | ||
| <label for="email" class="sr-only">Email</label> | ||
| <input type="email" name="email" class="form-control" placeholder="Email address"> | ||
| </div> | ||
| <div class="form-group mb-4"> | ||
| <label for="password" class="sr-only">Password</label> | ||
| <input type="password" name="password" class="form-control" placeholder="***********"> | ||
| </div> | ||
| <input class="btn btn-block login-btn mb-4" type="submit" > | ||
| </form> | ||
|
|
||
|
|
||
|
|
||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </main> | ||
|
|
||
| {% if messages %} | ||
| Messages | ||
|
|
||
| {{messages}} | ||
| {% endif %} | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> | ||
| <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> | ||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> | ||
|
|
||
|
|
||
|
|
||
|
|
||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | ||
| <title>Sign up</title> | ||
| </head> | ||
| <body> | ||
| <h2 class="text-center pt-4"> Welcome to my website </h2> | ||
| <!-- <form method="POST" > | ||
| {% csrf_token %} | ||
|
|
||
| <input type="email" name="email" placeholder="enter your email"> | ||
| <input type="password" name="password" placeholder="enter your password"> | ||
| <input type="submit"> | ||
|
|
||
| </form> --> | ||
|
|
||
| <main class="d-flex align-items-center min-vh-100 py-3 py-md-0"> | ||
| <div class="container"> | ||
| <div class="card login-card"> | ||
| <div class="row no-gutters"> | ||
| <div class="col-md-5"> | ||
| <img src="https://source.unsplash.com/tMI2_-r5Nfo/400x400" alt="login" class="login-card-img"> | ||
|
|
||
| </div> | ||
| <div class="col-md-7"> | ||
| <div class="card-body"> | ||
|
|
||
| <b> <p class="login-card-description text-center">Sign Up to your account</p> </b> | ||
| <form method="POST"> | ||
| {% csrf_token %} | ||
| <div class="form-group"> | ||
| <label for="email" class="sr-only">Email</label> | ||
| <input type="email" name="email" class="form-control" placeholder="Email address"> | ||
| </div> | ||
| <div class="form-group mb-4"> | ||
| <label for="password" class="sr-only">Password</label> | ||
| <input type="password" name="password" class="form-control" placeholder="***********"> | ||
| </div> | ||
| <input class="btn btn-block login-btn mb-4" type="submit" > | ||
| </form> | ||
|
|
||
|
|
||
|
|
||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </main> | ||
|
|
||
| <div style="background-color: blue;"> | ||
| {% if messages %} | ||
| Messages | ||
|
|
||
| {{messages}} | ||
| {% endif %} | ||
| </div> | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> | ||
| <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> | ||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> | ||
|
|
||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Welcome</title> | ||
| </head> | ||
| <body> | ||
| Welcome back {{messages}} | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from django.test import TestCase | ||
|
|
||
| # Create your tests here. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from django.urls import path | ||
| from . import views | ||
| from django.conf.urls import url, include | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path('login/', views.login), | ||
| path('signup/', views.signup), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from django.shortcuts import render | ||
| from .models import User | ||
| # Create your views here. | ||
|
|
||
|
|
||
| def login(request): | ||
| if request.method == 'GET': | ||
| return render(request,'signin.html') | ||
| else: | ||
| print(request.POST) | ||
| """ | ||
| LOGIN LOGIC | ||
| 1. email exists or not | ||
| 2. password is correct or not | ||
| """ | ||
| userData = User.objects.filter(email = request.POST["email"],password=request.POST["password"]) | ||
| if userData.exists(): | ||
| print("user successfully logged in ") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick-Note: This print will show data on cmd only and not a webpage. |
||
| print("userdata",userData[0]) | ||
| context={"messages":f"welcome {userData[0].email}"} | ||
|
|
||
| return render(request,'welcome.html',context=context) | ||
| else: | ||
| print("invalid credentials") | ||
| context={"messages":"invalid credentials"} | ||
| return render(request,'signin.html',context) | ||
|
|
||
|
|
||
| def signup(request): | ||
| if request.method == 'GET': | ||
| return render(request,'signup.html') | ||
| else: | ||
| """ | ||
| IT means request.method is POST | ||
| MAIN SIGNUP LOGIC | ||
| """ | ||
| print(request.POST) | ||
| """ | ||
| select * from user where email = request.POST["email"]; | ||
| """ | ||
| if User.objects.filter(email = request.POST["email"]).exists(): | ||
| context={"messages":"This email is already registered"} | ||
| return render(request,'signup.html',{"context":context}) | ||
| else: | ||
| if len(request.POST["password"]) > 6: | ||
| print("ok password length is greator then 6") | ||
| """ | ||
| insert into User values(request.POST["email"],request.POST["password"]) | ||
| """ | ||
| User.objects.create(email = request.POST["email"],password=request.POST["password"]) | ||
| print("user successfully created") | ||
| context={"messages":"user successfully created"} | ||
|
|
||
| return render(request,'signup.html',context) | ||
|
|
||
| else: | ||
| print("password is not ok") | ||
| context={"messages":"password length is short"} | ||
| return render(request,'signup.html',{"messages":"password length is short"}) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from django.contrib import admin | ||
|
|
||
| # Register your models here. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class BlogConfig(AppConfig): | ||
| name = 'blog' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Generated by Django 3.1.2 on 2021-06-27 05:58 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='blog', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('title', models.CharField(max_length=100)), | ||
| ('description', models.TextField()), | ||
| ], | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Generated by Django 3.1.2 on 2021-07-04 07:09 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ('blog', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='blog', | ||
| name='user_id', | ||
| field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from django.db import models | ||
| from django.contrib.auth.models import User | ||
|
|
||
| # Create your models here. | ||
| class blog(models.Model): | ||
| title = models.CharField(max_length=100) | ||
| description = models.TextField() | ||
|
|
||
| user_id = models.ForeignKey(User,on_delete=models.CASCADE,null=True,blank=True) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change user_id to created_by or author , that will look better. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
email is not a defined field in UserCreationForm , you'll have to create it statically and then add it in fields.
Refer this for help:
https://stackoverflow.com/questions/48049498/django-usercreationform-custom-fields