0

Пытаюсь добавить возможно редактирования таблицы, но выдает ошибку:

Generic detail view GroupUpdateView must be called with either an object pk or a slug in the URLconf.

HTML code:

{% extends 'main/doki.html' %}
{% load static %}

{% block content %}

    <div class="groupcont">
            <div class="disian-camp">
                <a href="{% url 'create2' %}"><li><button class="btn btn-info"><i class "fas fa-plus-circle"><i> Добавить новую таблицу</button></li></a>
                    <table class="disian-campe">
                        <thead>
                            <tr>
                                <th>id</th>
                                <th>Номер группы</th>
                                <th>ФИО Куратора</th>
                                <th>Редактирование</th>
                                <th>Удаление</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for el in groups %}
                                <tr>
                                    <td>{{ el.titleid }}</td>
                                    <td>{{ el.titlegroup1 }}</td>
                                    <td>{{ el.titlecurator }}</td>
                                    <td><a href="{% url 'create2' %}">Редактировать</a></td>
                                    <td><a href="">Удалить</a></td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
            </div>


{% endblock %}

second HTML code:

{% extends 'main/doki.html' %}
{% load static %}

{% block title %} Страница добавления таблиц {% endblock %}

{% block content %}
    <div class="createtable2">
        <style>
        form {
            margin-left: 32%;
            width: 500px;
            text-align: center;
        }
        </style>
            <form method="post">
                {% csrf_token %}
                {{ form.titleid }} <br>
                {{ form.titlegroup1 }} <br>
                {{ form.titlecurator }} <br>
                <button class "btn btn-success" type="submit">Добавить таблицу</button>
            </form>
    </div>


{% endblock %}

urls.py code:

from django.urls import path
from .import views

urlpatterns = [
    path('', views.registration, name='home'),
    path('group', views.about, name='group'),
    path('students', views.students, name='students'),
    path('create', views.create, name='create'),
    path('create2', views.create2, name='create2'),
    path('group/update', views.GroupUpdateView.as_view(), name='group/update'),
    path('student/update', views.StudentUpdateView.as_view(), name='student/update')
]

views.py code:

from django.shortcuts import render, redirect
from .models import group
from .models import studentes
from .forms import studentForm
from .forms import groupForm
from django.views.generic import DetailView, UpdateView


def registration(request):
    return render(request, 'main/registration.html')

def about(request):
    groups = group.objects.all()
    return render(request, 'main/group.html', {'groups':groups})

def students(request):
    studentss = studentes.objects.all()
    return render(request, 'main/students.html', {'studentss':studentss})


def create(request):
    error = ''
    if request.method == 'POST':
        form = studentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('student')
        else:
            error = 'Ошибка формы. Пожалуйста, проверьте правильность веденных данных'

    form = studentForm()

    data = {
        'form': form,
        'error': error
    }

    return render(request, 'main/create.html', data)

def create2(request):
    error = ''
    if request.method == 'POST':
        form = groupForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('group')
        else:
            error = 'Ошибка формы. Пожалуйста, проверьте правильность веденных данных'
        
    form = groupForm()

    data = {
        'form': form,
        'error': error
    }


    return render(request, 'main/create2.html', data)

class StudentUpdateView(UpdateView):
    model = studentes
    template_name = 'main/create.html'

    fields = ('titleid2', 'titlegroup2', 'titlestudent', 'titlephone', 'titleemail')

class GroupUpdateView(UpdateView):
    model = group
    template_name = 'main/create2.html'

    fields = ('titleid', 'titlegroup1', 'titlecurator')

1 ответ 1

0

Проблема заключается в том, что Вы не передаете идентификатор группы в urls.py

Должно быть так:

path('group/<int:pk>/update/', views.GroupUpdateView.as_view(), name='group/update'),

По умолчанию UpdateView ищет конкретный объект по полю PK.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.