Sunday, October 11, 2020

Re: How to Add to Django Administration Using Code

Hi do you hire contract based python/django freelancer?
 I can help you in this and related tasks  
Best Regards, 
Divyesh Khamele

On Sun, 11 Oct 2020, 9:59 pm Let's Get Going, <plyby171@gmail.com> wrote:
Hi everyone. I have created a Django chat app for testing. Two days ago it was working fine. But from yesterday it is not working as expected. I don't know where the mistake is.
Can anyone help me with that please?
I am pasting form, models,signup html page and views code.
or if anyone want to help me live then I can share the link of live share or anydesk code.

#Django-Form

from .models import User_Info
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms


class updateForm(ModelForm):
    
    class Meta:
        model=User_Info
        fields='__all__'
        exclude=['user']


class CustomUserCreateForm(UserCreationForm):
    class Meta:
        model=User
        fields=['username','password1','password2','email','first_name','last_name']

    def same_password(self):
        password1=self.cleaned_data['password1']
        password2=self.cleaned_data['password2']
        try:
            password1=password2
        except:
            raise forms.ValidationError('Passwords are not same.')
        return password1

   


#Django-Views

from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth import login,logout,authenticate
from .models import  User_Info
from django.contrib import messages
from django.contrib.auth.models import User
from . import forms

# Create your views here.

def index(request):
    return render(request,'LC/index.html')

def login_view(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('loginForm'))
    return HttpResponseRedirect(reverse('my_profile'))

def login_request(request):
    if request.method=='POST':
        username=request.POST['username']
        password=request.POST['password']
        user=authenticate(request,username=username,password=password)
        if user is not None:
            login(request,user)
            return HttpResponseRedirect(reverse('login'))
        else:
            return render(request,'LC/login.html',{
                'message':'Invalid Credentials.',
                'create_message':'Not registered?'
                
            })
    return render(request,'LC/login.html',{'message':'Login'})

def signup_view(request):
    form=forms.CustomUserCreateForm()
    if request.method=='POST':
        form=forms.CustomUserCreateForm(request.POST)
        if form.is_valid():
            username=request.POST['username']
            password=request.POST['password1']
            form.save()
            user=authenticate(request,username=username,password=password)
            if user is not None:
                login(request,user)
            return HttpResponseRedirect(reverse('my_profile'))
        else:
            return render(request,'LC/signup.html',{'form':form,'message':'Fill all the fields.'})
    return render(request,'LC/signup.html',{'form':form})

def my_profile(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('index'))
    form=forms.updateForm(instance=request.user.user_info)
    if request.method=="POST":
        form=forms.updateForm(request.POST,request.FILES,instance=request.user.user_info)
        if form.is_valid():
            form.save()
    return render(request,'LC/my_profile.html',{'form':form})

def users(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('index'))
    users=User.objects.all()
    return render(request,'LC/users.html',{'users':users})

def chats(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('index'))
    return render(request,'LC/chats.html')

def logout_view(request):
    logout(request)
    return render(request,'LC/login.html',{'message':'Logged Out.'})

#Django-Models

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
# Create your models here.


class User_Info(models.Model):
    user=models.OneToOneField(User,null=True,on_delete=models.CASCADE)
    
    about=models.TextField(null=True,max_length=1000)
    profile_pic=models.ImageField(default='defaultUser.png',null=True,blank=True)
    
    
def create_info(sender,**kwargs):
    if kwargs['created']:
        user_info=User_Info.objects.create(user=kwargs['instance'])

post_save.connect(create_info,sender=User)


#Html- Signup Form

{%load static%}
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="shortcut icon" href="{% static 'LC/images/logo.png'%}" type="image/x-icon">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sign Up | Let's Chat</title> <link href="https://fonts.googleapis.com/css2?family=PT+Serif&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="{%static 'LC/css/my_profile.css'%}">
</head>
<body>
  
{%if message%}
<p>{{message}}</p>
{%endif%}
  <form action="{% url 'signup'%}" method="POST">
    {%csrf_token%}
    {{form.username.label}} 
    {{form.username}}<br>
    {{form.first_name.label}} 
    {{form.first_name}}<br>
    {{form.last_name.label}} 
    {{form.last_name}}<br>
    {{form.email.label}} 
    {{form.email}} <br>
    {{form.password1.label}} 
    {{form.password1}} <br>
    {{form.password2.label}}
    {{form.password2}} <br>
    <button>Create</button>
  </form>
  <script src="{%static 'LC/js/chats.js'%}"></script>
</body>
</html>




Please Help me if you can. I can share Live Share link or anydesk code if needed.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEvQWLMxYCBgdqaTkv%2B_w5DHXrCnxO45WUqXXJY3_mE979rFHw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH9mneW6Bb6Ne0KN58OGJ9ko29rid%3DqmPSimA0iQDT9%3DkMTZ9Q%40mail.gmail.com.

No comments:

Post a Comment