How to Add to Django Administration Using Code

114 views
Skip to first unread message

Lightning Bit

unread,
Sep 20, 2020, 11:35:05 AM9/20/20
to Django users
Hi all, 

I am trying to figure out how to add "Customers" using code. Please look at the administration layout: 

HELP.png

I was able to add to "Groups" using the following code in "views.py": 

@unauthenticated_user
def registerPage(request):

            form = CreateUserForm()

            if request.method == 'POST':
                form = CreateUserForm(request.POST)
                if form.is_valid():
                    user = form.save()
                    username = form.cleaned_data.get('username')
                    
                    group = Group.objects.get(name='customer')
                    user.groups.add(group)
                    


                    messages.success(request, 'BOLT Force account was created for' + username )
                    return redirect('login')

            data= cartData(request)

            items=data['items']
            order=data['order']
            cartItems=data['cartItems']

            products = Product.objects.all()
            context = {'products':products, 'items':items, 'order':order, 'cartItems':cartItems, 'form':form}
            return render(request, 'store/register.html', context)

However, I am unsure of how to add to the "Customers" section of the picture above. I have tried almost every technique with no luck. I wanted both the "Groups" and the "Customers" to have the same registered person added at once. Would anyone happen to know how to fix this? 

Thank you!

coolguy

unread,
Sep 21, 2020, 1:28:38 PM9/21/20
to Django users
do want to add the picture file to the customer application or want to see the picture in admin?

coolguy

unread,
Sep 21, 2020, 1:54:34 PM9/21/20
to Django users

further to my previous comments please share the your related model code to make discussion interesting...
On Sunday, September 20, 2020 at 11:35:05 AM UTC-4 Lightning Bit wrote:

Let's Get Going

unread,
Oct 11, 2020, 12:30:06 PM10/11/20
to django...@googlegroups.com
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.

Dvs Khamele

unread,
Oct 11, 2020, 12:57:30 PM10/11/20
to django...@googlegroups.com
Hi do you hire contract based python/django freelancer?
 I can help you in this and related tasks  
Best Regards, 
Divyesh Khamele

--
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...@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.
Reply all
Reply to author
Forward
0 new messages