Не могу выстроить путь к странице django

Рейтинг: 0Ответов: 1Опубликовано: 02.03.2023

models:

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
email = models.EmailField(max_length=35)
password = models.CharField(max_length=35)
city = models.CharField(max_length=35, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
phone = models.CharField(max_length=10, blank=True, null=True)
is_active = models.BooleanField(default=False)

def get_absolute_url(self):
    return reverse('top_up_your_balance', args=[str(self.id)])


class Balance(models.Model):
    profile = models.ForeignKey('Profile', on_delete=models.CASCADE)
    balance = models.DecimalField(max_digits=10, decimal_places=2, default=0)

views

class ReplenishBalanceView(UpdateView):
    model = Balance
    form_class = ReplenishBalanceForm
    template_name = 'market/top_up_balance.html'
    success_url = reverse_lazy('account')

def get_object(self):
    profile_id = self.kwargs.get('profile_id')
    profile = get_object_or_404(Profile, id=profile_id)
    balance, created = Balance.objects.get_or_create(profile=profile)
    return balance

def form_valid(self, form):
    balance = form.cleaned_data['balance']
    balance_obj = self.get_object()
    balance_obj.balance += balance
    balance_obj.save()
    return super().form_valid(form)

class AccountView(ListView):
    model = Profile
    template_name = 'market/account.html'
    context_object_name = 'acc'

templates account.html

<
!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Данные пользователя</title>
</head>
<body>
<h2>Данные о пользователе</h2>
<ul>

    <li>Ник пользователя: <strong>{{ request.user.username }}</strong></li>
    <li>Почта: <strong>{{ request.user.profile.email }}</strong></li>
    <li>Имя пользователя: <strong>{{ request.user.profile.first_name }}</strong></li>
    <li>Фамилия пользователя: <strong>{{ request.user.profile.last_name }}</strong></li>
    <li>Дата рождения: <strong>{{ request.user.profile.date_of_birth }}</strong></li>
    <li>Город проживания: <strong>{{ request.user.profile.city }}</strong></li>
    <li>Номер телефона: <strong>{{ request.user.profile.phone }}</strong></li>
    <li>Баланс: <strong>{{ request.user.profile.balance }}</strong></li>

</ul>

<a href="{% url 'top_up_your_balance' profile.id %}">Пополнить баланс</a>

</body>
</html>

top_up_your_balance.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h2>Пополнение баланса</h2>

  <form method="post">
    {% csrf_token %}
    <label for="amount">Amount:</label>
    <input type="text" id="amount" name="amount">
    <br>
    <input type="submit" value="Top up">
  </form>
</body>
</html>

forms

class ReplenishBalanceForm(forms.ModelForm):
    class Meta:
        model = Balance
        fields = ['balance']

urls

urlpatterns = [
path('', Listofstores.as_view(), name='home'),
path('login', AppLogin.as_view(), name='login'),
path('logout', LogoutNewsView.as_view(), name='logout'),
path('register', AnotherRegisterView.as_view(), name='another_register'),
path('account', AccountView.as_view(), name='account'),
path('account/<int:profile_id>/top_up_your_balance/', ReplenishBalanceView.as_view(), name='top_up_your_balance'),

]

Я хочу чтобы пользователь мог перейти по ссылке пополнить баланс и пополнил свой баланс но у меня выходит такая ошибка: Reverse for 'top_up_your_balance' with arguments '('',)' not found. 1 pattern(s) tried: ['account/(?P<profile_id>[0-9]+)/top_up_your_balance/\Z']
За ранее спасибо за помощь!!!

Ответы

▲ 0

В тэге нужно присваивать id, поправил:

<a href="{% url 'top_up_your_balance' profile_id=profile.id %}">Пополнить баланс</a>