Я учусь Django 2.2, используя диаграмму js, я пытаюсь отобразить свои навыки пользователя при изменении идентификатора в URL. У меня 2 пользователя, оно работает для первого, но не для второго.
Вот ошибка:
Profile matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8080/skills/2/
Django Version: 2.2
Exception Type: DoesNotExist
Exception Value:
Profile matching query does not exist.
Вот моя модель профиля, Profile => models.py:
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import FileExtensionValidator
# Create your models here.
class Profile(models.Model):
name = models.ForeignKey(User, on_delete=models.CASCADE)
website = models.URLField(blank=True)
avatar = models.ImageField(upload_to='uploads/img', validators=[FileExtensionValidator(allowed_extensions=['png'])], blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
@property
def get_created(self):
return self.created.strftime("%m/%d/%Y, %H:%M:%S")
def __str__(self):
return "{}-{}".format(self.name, self.get_created)
Вот мои модели навыков, Навыки => models.py
from django.db import models
from profiles.models import Profile
from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.
class Skill(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
name = models.CharField(max_length=220)
score = models.PositiveIntegerField(
validators=[MinValueValidator(1), MaxValueValidator(5)])
def __str__(self):
return "{}-{}-{}".format(self.user, self.name, self.score)
Вот мои URL навыков, Навыки => urls.py:
from django.urls import path
from .views import skill_view, UserChartView
app_name = 'skills'
urlpatterns = [
path('', skill_view, name='my-skills'),
path('<profile_id>/', UserChartView.as_view(), name='user-skills'),
]
Вот мой пользовательский вид, Skills => views.py:
from django.shortcuts import render, redirect
from django.forms import inlineformset_factory
from .models import Skill
from profiles.models import Profile
from django.views.generic import TemplateView
# Create your views here.
class UserChartView(TemplateView):
template_name = 'skills/user_skills.html'
def get_object(self):
pk = self.kwargs.get('profile_id')
return pk
def get_context_data(self, **kwargs):
context = super(UserChartView, self).get_context_data(**kwargs)
profile = Profile.objects.get(name__id=self.get_object())
qs = profile.skill_set.all()
context["qs"] = qs
return context