Я заметил, что когда я вошел как мой профиль (http://127.0.0.1: 8000 / user / admin / ) и я go в свой шаблон, где у меня есть список всех пользователей (http://127.0.0.1: 8000 / users / ) и я нажимаю на другого пользователя (mattia_mibe), чтобы увидеть его / ее профиль, который поменял мою учетную запись, поэтому я не вижу его / ее профиль, но вижу свой профиль ( admin) со всеми моими описаниями, но в то же время URL-адрес http://127.0.0.1: 8000 / user / mattia_mibe / и он распознает @mattia_mibe как пользователей, даже то, что он отображает, является описанием моего профиля (admin).
Это немного сложно объяснить, я выкладываю здесь несколько фотографий на случай, если я не объяснил проблему четко.
core / urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.homepage, name='homepage'),
path("users/", views.UserList.as_view(), name='user_list'),
path("user/<username>/", views.userProfileView, name='user_profile'),
]
core / views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView
# Create your views here.
from quiz.models import Questions
from jobs.models import post_job
def homepage(request):
return render(request, 'core/homepage.html')
def userProfileView(request, username):
user= get_object_or_404(User, username=username)
jobs = post_job.objects.all()
categories = Questions.CAT_CHOICES
scores = []
for category in categories:
score = Questions.objects.filter(category=category[0], student= request.user).count()
scores.append(score)
context = {
'user' : user, 'categories_scores' : zip( categories,scores),
'jobs': jobs
}
return render(request, 'core/user_profile.html' , context)
class UserList(LoginRequiredMixin, ListView):
model = User
template_name = 'core/users.html'
account / views.py
from django.shortcuts import render, HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from accounts.forms import FormRegistrazione
# Create your views here.
def registrazioneView(request):
if request.method == "POST":
form = FormRegistrazione(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
email = form.cleaned_data["email"]
password = form.cleaned_data["password1"]
User.objects.create_user(username=username, password=password, email=email)
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect("/")
else:
form = FormRegistrazione()
context = {"form": form}
return render(request, 'accounts/registrazione.html', context)
account / urls.py
from django.urls import path
from .views import registrazioneView
urlpatterns = [
path('registrazione/', registrazioneView, name='registration_view')
]
accounts / forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class FormRegistrazione(UserCreationForm):
email = forms.CharField(max_length=30, required=True, widget=forms.EmailInput())
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
Terminal
[25/Apr/2020 13:26:24] "GET /user/admin/ HTTP/1.1" 200 10824
[25/Apr/2020 13:26:31] "GET /users/ HTTP/1.1" 200 5301
[25/Apr/2020 13:26:37] "GET /user/mattia_mibe/ HTTP/1.1" 200 10822
[25/Apr/2020 13:26:47] "GET /accounts/logout/ HTTP/1.1" 200 4000
[25/Apr/2020 13:26:49] "GET /accounts/login/ HTTP/1.1" 200 5162
[25/Apr/2020 13:26:55] "POST /accounts/login/ HTTP/1.1" 302 0
[25/Apr/2020 13:26:55] "GET / HTTP/1.1" 200 5010
[25/Apr/2020 13:26:57] "GET /user/mattia_mibe/ HTTP/1.1" 200 6024
core / user_profile. html
{% extends 'base.html'%}
{% block content %}
<br>
<div class="card-header">
<h3> {% if request.user == user %} Il tuo {% endif %} Profilo Utente </h3>
<br>
<h1> Ciao Studente: @{{ user }}</h1>
</div>
<br>
<br>
{% for category,score in categories_scores %}
<div class="card">
<div class="card-header">
{{ category.1 }} Score
</div>
<div class="card-body">
<h5 class="card-title">CONGRATS!</h5>
<p class="card-text"> Here your SCORE: </p>
<a href="#" class="btn btn-primary">{{ score }}</a>
{% if score > 1 %}
<br>
<br>
<div class="progress">
<div class="progress-bar bg-success" role="progressbar" style="width: 99%" aria-valuenow="99" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<p class="card-text"> Congratulations! Now you can apply to:</p>
<br>
{% for job in jobs %}
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
{{job.posizione}} <strong> {{job.nome_azienda}} </strong>
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<ul>
<li> {{job.descrizione}} </li>
<li> {{job.email_referente}} </li>
</ul>
</div>
</div>
</div>
{% endfor %}
{% else %}
<br>
<br>
<div class="progress">
<div class="progress-bar bg-warning" role="progressbar" style="width: 25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<p class="card-text"> Still not meet the requirements to apply, keep going! </p>
{% endif %}
</div>
</div>
<br>
{% endfor %}
{% endblock content %}