M
M
Mikhail Bolev2021-03-20 15:04:07
Django
Mikhail Bolev, 2021-03-20 15:04:07

How to get current user data on DetailView?

Good afternoon! I'm trying to get the data of the current user to display on the page. The link I specified 127.0.0.1:8000/cabinet/1 gives an error. Why?

models.py (crm)

from django.contrib.auth.models import AbstractUser

# Create your models here.
class CustomUser(AbstractUser):
    pass


urls.py (third line in pattern)
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth import login, logout
from crm.views import PersonalAccount, index, Registr
from orders.views import admin_order_detail

urlpatterns = [
    path('admin/orders/order/<order_id>/change/', admin_order_detail, name='admin_order_detail'),
    path('admin/', admin.site.urls),
    path('cabinet/<pk>', PersonalAccount.as_view(), name='pers_account'),
    path('registr/', Registr.as_view(), name='registr'),
    path('accounts/', include("django.contrib.auth.urls")),
    path('accounts/login/', login, name='login'),
    path('accounts/logout/', logout, name='logout'),
    path('product/', include('shop.urls', namespace='shop')),
    path('orders/', include('orders.urls', namespace='orders')),
    path('cart/', include('cart.urls', namespace='cart')),
    path('coupons/', include('coupons.urls', namespace='coupon')),
    path('', index, name='index'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


views.py (crm)
from django.views.generic import FormView
from django.views.generic import DetailView
from django.urls import reverse_lazy
from .forms import CustomUserCreationForm
from django.contrib.auth import login, authenticate
from django.shortcuts import render
from shop.models import Product
from crm.models import CustomUser

# Create your views here.
def index(request):
#    slider_list = CmsSlider.objects.all()
    products = Product.objects.all()
    dict_obj = {'products': products}
    return render(request, './index.html', dict_obj)

class PersonalAccount(DetailView):
    model = CustomUser 
    template_name = 'pers_account.html'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Bolev, 2021-03-20
@Scorpion_MB

I do not know what it is connected with. Replaced the input argument in the template with user.id (it used to be CustomUser.pk, but the address ' 127.0.0.1:8000/cabinet/1 ' appeared). Now everything works.

D
Dr. Bacon, 2021-03-20
@bacon

1. what is the error?
2. for the DetailView template, the selected object is available 3. in the template, the current user is always available to you 4. in the views themselves, the current user is available in request.user (if request is explicitly passed to the method) or self.request.user {{ object }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question