SITUATION
- I am modifying this GitHub project from this YouTube Series and this is a demo how the original application runs.
- My goal is to add settings option for Merchant(in the code Admin or AdminUser) accounts in the marketplace, because in the original project only buyers have the option to add image and their contact details.
CODE
model.py
#database table create
class Customer(models.Model):
#empty fields accept - null=True
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
profile_pic = models.ImageField(default="profile1.png", null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
#show customer name in admin panel
def __str__(self):
return self.name
class Adminuser(models.Model):
#empty fields accept - null=True
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
profile_pic = models.ImageField(default="profile1.png", null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
#show customer name in admin panel
def __str__(self):
return self.name
url.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
...
path('customer/<str:pk_test>/', views.customer, name="customer"),
path('adminuser/<str:pk_test>/', views.adminuser, name="adminuser"),
...
]
views.py
#CUSTOMER_ONLY PROFILE SETTINGS
@login_required(login_url='login')
@allowed_users(allowed_roles=['customer'])
def accountSettings(request):
customer = request.user.customer
form = CustomerForm(instance=customer)
if request.method == 'POST':
form = CustomerForm(request.POST, request.FILES,instance=customer)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'accounts/account_settings.html', context)
#ADMIN_ONLY PROFILE SETTINGS
@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def adminSettings(request):
admin = request.user.admin
form = AdminForm(instance=admin)
if request.method == 'POST':
form = AdminForm(request.POST, request.FILES,instance=admin)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'accounts/account_admin_settings.html', context)
forms.py
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from .models import *
class CustomerForm(ModelForm):
class Meta:
model = Customer
fields = '__all__'
exclude = ['user']
class AdminForm(ModelForm):
class Meta:
model = Adminuser
fields = '__all__'
exclude = ['user']
navbar.html
...
<li class="nav-item">
<a class="nav-link" href="{% url 'adminsettings' %}">SettingsAdmin</a>
</li>
...
account_admin_settings.html
{% extends 'accounts/main.html' %}
{% load static %}
{% block content %}
<style>
.profile-pic{
max-width: 200px;
max-height:200px;
margin: 0 auto;
border-radius: 50%;
}
</style>
<!-- -->
<br>
<div class="row">
<div class="col-md-3">
<div class="card card-body">
<a class="btn btn-warning" href="{% url 'dashboard' %}"> ← Back to Profile</a>
<hr>
<h3 style="text-align: center">Account Settings</h3>
<hr>
<img class="profile-pic" src="{{request.user.customer.profile_pic.url}}" >
</div>
</div>
<div class="col-md-9">
<div class="card card-body">
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input class="btn btn-primary" type="submit" name="Update Information">
</form>
</div>
</div>
</div>
ERROR
AttributeError at /accounts/adminsettings/
'User' object has no attribute 'admin'
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/adminsettings/
Django Version: 3.0
Exception Type: AttributeError
Exception Value:
'User' object has no attribute 'admin'
Exception Location: /Users/computer/ven/lib/python3.7/site-packages/django/utils/functional.py in inner, line 225
Python Executable: /Users/computer/ven/bin/python3
Python Version: 3.7.3
Python Path:
['/Users/computer/project',
'/Users/computer/anaconda3/lib/python37.zip',
'/Users/computer/anaconda3/lib/python3.7',
'/Users/computer/anaconda3/lib/python3.7/lib-dynload',
'/Users/computer/ven/lib/python3.7/site-packages']
Server time: Wed, 17 Jun 2020 22:17:12 +0000
TRIED SOLUTIONS
- My error is not at the login and I am not sure where it is - Attribute error: 'User' object has no attribute 'is_admin'
- I have done migration and make migration before - Django - No such table: main.auth_user__old
- buyers can access the settings with their own model so this self. does not solves my case django 'User' object has no attribute 'user'
- I don't write over a .save method - 'NoneType' object has no attribute 'user' Django
- It should have the same lvl of instances like in the customer model it is just a different user priority lvl group - Django 'User' object has no attribute 'Goals'
- This is python 2.7 and Django 1.5 outdated question and answer to begin with - Django Admin Issue - 'NoneType' object has no attribute 'user'
- This has nothing to to do with my case - Django Admin Action: 'QuerySet' object has no attribute 'user'
- All my middle wears functioning fine - Django admin error 'WSGIRequest' object has no attribute 'user
- Outdated Q & A and my middle wears works - 'WSGIRequest' object has no attribute 'user' Django admin
@allowed_users(allowed_roles=['customer'])
change to@allowed_users(allowed_roles=['customer','admin'])
to have everything in to 1 function indef accountSettings(request):
gives error- added
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE, related_name='admin')
- ERROR:
RelatedObjectDoesNotExist at /accounts/adminsettings/ User has no admin.
- ERROR:
- After I have added
request.user.adminuser
to the view function- ERROR
AttributeError at /accounts/adminsettings/ 'User' object has no attribute 'adminuser'
- ERROR
- Where do I need to use this How do you catch this exception? in the view or in the model? And How? :
MY FUNCTION ANME
try:
# MY ORIGINAL CODE
except ObjectDoesNotExist:
# and what goes here?????
- @xyres updated view.py def adminSettings(request):
#ADMIN_ONLY PROFILE SETTINGS ''' '''
@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def adminSettings(request):
try:
admin = requset.user.admin
form = AdminForm(instance=admin)
if request.method == 'POST':
form = AdminForm(request.POST, request.FILES,instance=admin)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'accounts/account_admin_settings.html', context)
except ObjectDoesNotExist:
print("Either the entry or blog doesn't exist.")
return render(request, 'accounts/account_admin_settings.html', context)
- ERROR:
File "/Users/computer/project/accounts/views.py", line 106 form = AdminForm(instance=admin) ^
- Hanged back the views.py and models.py back to it's original state and now I get a new error (after makemigration migrate changes)
NameError at /accounts/adminsettings/
name 'requset' is not defined