V
V
Vitaly Ananiev2022-01-03 17:20:13
Django
Vitaly Ananiev, 2022-01-03 17:20:13

How to display the form in Detail so that the form fields display information associated with the slug?

How to display the form in the class SellAvailabilityDetail so that the form fields display information tied to the slug, in my case slug=Games.id_num?
my project:

---models---
from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType

User = get_user_model()


class Servers(models.Model):
    name = models.CharField(max_length=255, verbose_name='Все сервера', blank=True, null=True)
    id_num = models.PositiveIntegerField(default=0, verbose_name='id-номер')

    def __str__(self):
        return self.name


class Games(models.Model):
    name = models.CharField(max_length=255, verbose_name='Наиминование игры')
    id_num = models.PositiveIntegerField(default=0, verbose_name='id-номер')
    game_currency = models.CharField(max_length=50, verbose_name='Игровая валюта', blank=True, null=True)
    game_account = models.CharField(max_length=50, verbose_name='Аккаунты', blank=True, null=True)
    game_items = models.CharField(max_length=50, verbose_name='Предметы', blank=True, null=True)
    game_services = models.CharField(max_length=50, verbose_name='Услуги', blank=True, null=True)

    sell_currency = models.CharField(max_length=50, verbose_name='Продать игровую валюту', blank=True, null=True)
    sell_accounts = models.CharField(max_length=50, verbose_name='Продать аккаунты', blank=True, null=True)
    sell_items = models.CharField(max_length=50, verbose_name='Продать предметы', blank=True, null=True)
    sell_services = models.CharField(max_length=50, verbose_name='Продать услуги', blank=True, null=True)

    server = models.ManyToManyField(Servers, verbose_name='Сервер', blank=True)
    image = models.ImageField(verbose_name='Изображение')
    price_per = models.CharField(max_length=50, verbose_name='Цена за:', blank=True, null=True)
    description = models.TextField(verbose_name='Описание', max_length=500, blank=True, null=True)

    def currency_absolute_url(self):
        return reverse('games_detail', kwargs={'slug': self.id_num})

    def accounts_absolute_url(self):
        return reverse('accounts_detail', kwargs={'slug': self.id_num})

    def items_absolute_url(self):
        return reverse('items_detail', kwargs={'slug': self.id_num})

    def services_absolute_url(self):
        return reverse('services_detail', kwargs={'slug': self.id_num})

    def sell_game_currency_url(self):
        return reverse('sell_game_currency', kwargs={'slug': self.id_num})

    def __str__(self):
        return self.name


class Sell(models.Model):
    class Meta:
        abstract = True

    owner = models.ForeignKey('Customer', null=True, verbose_name='Продавец', on_delete=models.CASCADE)
    games = models.ForeignKey(Games, null=True, verbose_name='Игра', on_delete=models.CASCADE)
    server = models.ForeignKey(Servers, null=True, verbose_name='Сервер', on_delete=models.CASCADE)
    price = models.PositiveIntegerField(default=0, verbose_name='Цена', blank=True, null=True)

    def __str__(self):
        return f'{self.owner}'


class Availability(Sell):
    availability = models.PositiveIntegerField(default=0, verbose_name='Наличие игровой валюты', blank=True, null=True)

---views---
class SellAvailabilityDetail(FormView, Info):
    model = Games
    template_name = 'Pay/sell_availability_detail.html'
    slug_field = "id_num"
    context_object_name = 'games'
    form_class = AddAvailabilityForm
    success_url = reverse_lazy('base')

    def form_valid(self, form):
        Availability.objects.create(**form.cleaned_data)
        return redirect('base')

---utils---
from django.views.generic import DetailView
from .models import *
from .forms import *


class Info(DetailView):

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['availability'] = Availability.objects.all()
        context['accounts'] = Account.objects.all()
        context['items'] = Items.objects.all()
        context['services'] = Services.objects.all()
        context['form'] = AddAvailabilityForm

        return context

---urls---
from django.urls import path
from .views import (
    BaseView,
    GamesDetailView,
    AccountsDetailView,
    ItemsDetailView,
    ServicesDetailView,
    SellAvailabilityDetail,

)

urlpatterns = [
    path('', BaseView.as_view(), name='base'),
    path('currency/<int:slug>/', GamesDetailView.as_view(), name='games_detail'),
    path('accounts/<int:slug>/', AccountsDetailView.as_view(), name='accounts_detail'),
    path('items/<int:slug>/', ItemsDetailView.as_view(), name='items_detail'),
    path('services/<int:slug>/', ServicesDetailView.as_view(), name='services_detail'),
    path('sell_game_currency/<int:slug>', SellAvailabilityDetail.as_view(), name='sell_game_currency'),

---sell_availability_detail.html---
{% extends 'base.html' %}


{% block content %}


<form action="{% url 'sell_game_currency' slug=games.id_num %}" method="post">
    {% csrf_token %}
    {% for f in form %}
        <p><label class="" for="{{ f.id_for_label }}">{{f.label}}: </label>{{ f }}</p>
        <div class="">{{ f.errors }}</div>
    {% endfor %}
    <button type="submit">Добавить</button>
</form>

{% endblock content %}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question