Уведомления

Группа в Telegram: @pythonsu

#1 Апрель 9, 2020 14:09:37

7emsk
Зарегистрирован: 2020-04-09
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Django PYTHON

Здравствуйте ))
Столкнулся ошибками формы, уже 3 день лазию по форумам хоть бы кто нибудь адекватно помог мне в моей проблемы ((
После нажатия на ссылку добавить выскакивает ошибки и сайт не открывается((
Программа указывает на ошибку строки 33 в файле edit но после того как я перешел я не могу понять что делать ((
Вот сама ошибка

 return form_class (**self.get_form_kwargs())
Потом я удалил и сайт открылся но форма не передает данные на страницу ((
 return form_class
Файл views.py
 from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render
from django.views.generic.edit import CreateView
from .forms import BbForm
from .models import Bb
from .models import Rubric
def index (request):
    template = loader.get_template('Spam/index.html')
    bbs = Bb.objects.all()
    rubrics = Rubric.objects.all()
    context = {'bbs' : bbs, 'rubrics' : rubrics}
    return render(request, 'Spam/index.html', context)
    s = 'Список объявлений\r\n\r\n\r\n'
    for bb in Bb.objects.order_by('-published'):
        s += bb.title + '\r\n' + bb.content + '\r\n\r\n'
    return HttpResponse(template.render(context, request))
def by_rubric(request, rubric_id):
    bbs = Bb.objects.filter(rubric=rubric_id)
    rubrics = Rubric.objects.all()
    current_rubric = Rubric.objects.get (pk=rubric_id)
    context = {'bbs' : bbs, 'rubrics' : rubrics, 'current_rubric' : current_rubric}
    return render(request, 'Spam/by_rubric.html', context)
class BbCreateView(CreateView):
    template_name = 'Spam/create.html'
    form_class = BbForm
    success_url = '/Spam/'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['rubrics'] = Rubric.objects.all()
        return context
Файл edit.py
 from django.core.exceptions import ImproperlyConfigured
from django.forms import models as model_forms
from django.http import HttpResponseRedirect
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
from django.views.generic.detail import (
    BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin,
)
class FormMixin(ContextMixin):
    """Provide a way to show and handle a form in a request."""
    initial = {}
    form_class = None
    success_url = None
    prefix = None
    def get_initial(self):
        """Return the initial data to use for forms on this view."""
        return self.initial.copy()
    def get_prefix(self):
        """Return the prefix to use for forms."""
        return self.prefix
    def get_form_class(self):
        """Return the form class to use."""
        return self.form_class
    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        if form_class is None:
            form_class = self.get_form_class()
        return form_class (**self.get_form_kwargs())
    def get_form_kwargs(self):
        """Return the keyword arguments for instantiating the form."""
        kwargs = {
            'initial': self.get_initial(),
            'prefix': self.get_prefix(),
        }
        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs
    def get_success_url(self):
        """Return the URL to redirect to after processing a valid form."""
        if not self.success_url:
            raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
        return str(self.success_url)  # success_url may be lazy
    def form_valid(self, form):
        """If the form is valid, redirect to the supplied URL."""
        return HttpResponseRedirect(self.get_success_url())
    def form_invalid(self, form):
        """If the form is invalid, render the invalid form."""
        return self.render_to_response(self.get_context_data(form=form))
    def get_context_data(self, **kwargs):
        """Insert the form into the context dict."""
        if 'form' not in kwargs:
            kwargs['form'] = self.get_form()
        return super().get_context_data(**kwargs)
class ModelFormMixin(FormMixin, SingleObjectMixin):
    """Provide a way to show and handle a ModelForm in a request."""
    fields = None
    @property
    def get_form_class(self):
        """Return the form class to use in this view."""
        if self.fields is not None and self.form_class:
            raise ImproperlyConfigured(
                "Specifying both 'fields' and 'form_class' is not permitted."
            )
        if self.form_class:
            return self.form_class
        else:
            if self.model is not None:
                # If a model has been explicitly provided, use it
                model = self.model
            elif getattr(self, 'object', None) is not None:
                # If this view is operating on a single object, use
                # the class of that object
                model = self.object.__class__
            else:
                # Try to get a queryset and extract the model class
                # from that
                model = self.get_queryset().model
            if self.fields is None:
                raise ImproperlyConfigured(
                    "Using ModelFormMixin (base class of %s) without "
                    "the 'fields' attribute is prohibited." % self.__class__.__name__
                )
            return model_forms.modelform_factory(model, fields=self.fields)
    def get_form_kwargs(self):
        """Return the keyword arguments for instantiating the form."""
        kwargs = super().get_form_kwargs()
        if hasattr(self, 'object'):
            kwargs.update({'instance': self.object})
        return kwargs
    def get_success_url(self):
        """Return the URL to redirect to after processing a valid form."""
        if self.success_url:
            url = self.success_url.format(**self.object.__dict__)
        else:
            try:
                url = self.object.get_absolute_url()
            except AttributeError:
                raise ImproperlyConfigured(
                    "No URL to redirect to.  Either provide a url or define"
                    " a get_absolute_url method on the Model.")
        return url
    def form_valid(self, form):
        """If the form is valid, save the associated model."""
        self.object = form.save()
        return super().form_valid(form)
class ProcessFormView(View):
    """Render a form on GET and processes it on POST."""
    def get(self, request, *args, **kwargs):
        """Handle GET requests: instantiate a blank version of the form."""
        return self.render_to_response(self.get_context_data())
    def post(self, request, *args, **kwargs):
        """
        Handle POST requests: instantiate a form instance with the passed
        POST variables and then check if it's valid.
        """
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)
    # PUT is a valid HTTP verb for creating (with a known URL) or editing an
    # object, note that browsers only support POST for now.
    def put(self, *args, **kwargs):
        return self.post(*args, **kwargs)
class BaseFormView(FormMixin, ProcessFormView):
    """A base view for displaying a form."""
class FormView(TemplateResponseMixin, BaseFormView):
    """A view for displaying a form and rendering a template response."""
class BaseCreateView(ModelFormMixin, ProcessFormView):
    """
    Base view for creating a new object instance.
    Using this base class requires subclassing to provide a response mixin.
    """
    def get(self, request, *args, **kwargs):
        self.object = None
        return super().get(request, *args, **kwargs)
    def post(self, request, *args, **kwargs):
        self.object = None
        return super().post(request, *args, **kwargs)
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
    """
    View for creating a new object, with a response rendered by a template.
    """
    template_name_suffix = '_form'
class BaseUpdateView(ModelFormMixin, ProcessFormView):
    """
    Base view for updating an existing object.
    Using this base class requires subclassing to provide a response mixin.
    """
    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().get(request, *args, **kwargs)
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super().post(request, *args, **kwargs)
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
    """View for updating an object, with a response rendered by a template."""
    template_name_suffix = '_form'
class DeletionMixin:
    """Provide the ability to delete objects."""
    success_url = None
    def delete(self, request, *args, **kwargs):
        """
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        """
        self.object = self.get_object()
        success_url = self.get_success_url()
        self.object.delete()
        return HttpResponseRedirect(success_url)
    # Add support for browsers which only accept GET and POST for now.
    def post(self, request, *args, **kwargs):
        return self.delete(request, *args, **kwargs)
    def get_success_url(self):
        if self.success_url:
            return self.success_url.format(**self.object.__dict__)
        else:
            raise ImproperlyConfigured(
                "No URL to redirect to. Provide a success_url.")
class BaseDeleteView(DeletionMixin, BaseDetailView):
    """
    Base view for deleting an object.
    Using this base class requires subclassing to provide a response mixin.
    """
class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
    """
    View for deleting an object retrieved with self.get_object(), with a
    response rendered by a template.
    """
    template_name_suffix = '_confirm_delete'

Файл models.py
[code html]
from django.core.exceptions import ImproperlyConfigured
from django.forms import models as model_forms
from django.http import HttpResponseRedirect
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
from django.views.generic.detail import (
BaseDetailView, SingleObjectMixin, SingleObjectTemplateResponseMixin,
)


class FormMixin(ContextMixin):
"""Provide a way to show and handle a form in a request."""
initial = {}
form_class = None
success_url = None
prefix = None

def get_initial(self):
"""Return the initial data to use for forms on this view."""
return self.initial.copy()

def get_prefix(self):
"""Return the prefix to use for forms."""
return self.prefix

def get_form_class(self):
"""Return the form class to use."""
return self.form_class

def get_form(self, form_class=None):
"""Return an instance of the form to be used in this view."""
if form_class is None:
form_class = self.get_form_class()
return form_class (**self.get_form_kwargs())

def get_form_kwargs(self):
"""Return the keyword arguments for instantiating the form."""
kwargs = {
'initial': self.get_initial(),
'prefix': self.get_prefix(),
}

if self.request.method in ('POST', 'PUT'):
kwargs.update({
'data': self.request.POST,
'files': self.request.FILES,
})
return kwargs

def get_success_url(self):
"""Return the URL to redirect to after processing a valid form."""
if not self.success_url:
raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
return str(self.success_url) # success_url may be lazy

def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
return HttpResponseRedirect(self.get_success_url())

def form_invalid(self, form):
"""If the form is invalid, render the invalid form."""
return self.render_to_response(self.get_context_data(form=form))

def get_context_data(self, **kwargs):
"""Insert the form into the context dict."""
if 'form' not in kwargs:
kwargs['form'] = self.get_form()
return super().get_context_data(**kwargs)


class ModelFormMixin(FormMixin, SingleObjectMixin):
"""Provide a way to show and handle a ModelForm in a request."""
fields = None

@property
def get_form_class(self):
"""Return the form class to use in this view."""
if self.fields is not None and self.form_class:
raise ImproperlyConfigured(
"Specifying both 'fields' and 'form_class' is not permitted."
)
if self.form_class:
return self.form_class
else:
if self.model is not None:
# If a model has been explicitly provided, use it
model = self.model
elif getattr(self, 'object', None) is not None:
# If this view is operating on a single object, use
# the class of that object
model = self.object.__class__
else:
# Try to get a queryset and extract the model class
# from that
model = self.get_queryset().model

if self.fields is None:
raise ImproperlyConfigured(
"Using ModelFormMixin (base class of %s) without "
"the 'fields' attribute is prohibited." % self.__class__.__name__
)

return model_forms.modelform_factory(model, fields=self.fields)

def get_form_kwargs(self):
"""Return the keyword arguments for instantiating the form."""
kwargs = super().get_form_kwargs()
if hasattr(self, 'object'):
kwargs.update({'instance': self.object})
return kwargs

def get_success_url(self):
"""Return the URL to redirect to after processing a valid form."""
if self.success_url:
url = self.success_url.format(**self.object.__dict__)
else:
try:
url = self.object.get_absolute_url()
except AttributeError:
raise ImproperlyConfigured(
"No URL to redirect to. Either provide a url or define"
" a get_absolute_url method on the Model.")
return url

def form_valid(self, form):
"""If the form is valid, save the associated model."""
self.object = form.save()
return super().form_valid(form)


class ProcessFormView(View):
"""Render a form on GET and processes it on POST."""
def get(self, request, *args, **kwargs):
"""Handle GET requests: instantiate a blank version of the form."""
return self.render_to_response(self.get_context_data())

def post(self, request, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)

# PUT is a valid HTTP verb for creating (with a known URL) or editing an
# object, note that browsers only support POST for now.
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)


class BaseFormView(FormMixin, ProcessFormView):
"""A base view for displaying a form."""


class FormView(TemplateResponseMixin, BaseFormView):
"""A view for displaying a form and rendering a template response."""


class BaseCreateView(ModelFormMixin, ProcessFormView):
"""
Base view for creating a new object instance.

Using this base class requires subclassing to provide a response mixin.
"""
def get(self, request, *args, **kwargs):
self.object = None
return super().get(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
self.object = None
return super().post(request, *args, **kwargs)


class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
"""
View for creating a new object, with a response rendered by a template.
"""
template_name_suffix = '_form'


class BaseUpdateView(ModelFormMixin, ProcessFormView):
"""
Base view for updating an existing object.

Using this base class requires subclassing to provide a response mixin.
"""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super().get(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)


class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
"""View for updating an object, with a response rendered by a template."""
template_name_suffix = '_form'


class DeletionMixin:
"""Provide the ability to delete objects."""
success_url = None

def delete(self, request, *args, **kwargs):
"""
Call the delete() method on the fetched object and then redirect to the
success URL.
"""
self.object = self.get_object()
success_url = self.get_success_url()
self.object.delete()
return HttpResponseRedirect(success_url)

# Add support for browsers which only accept GET and POST for now.
def post(self, request, *args, **kwargs):
return self.delete(request, *args, **kwargs)

def get_success_url(self):
if self.success_url:
return self.success_url.format(**self.object.__dict__)
else:
raise ImproperlyConfigured(
"No URL to redirect to. Provide a success_url.")


class BaseDeleteView(DeletionMixin, BaseDetailView):
"""
Base view for deleting an object.

Using this base class requires subclassing to provide a response mixin.
"""


class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
"""
View for deleting an object retrieved with self.get_object(), with a
response rendered by a template.
"""
template_name_suffix = '_confirm_delete'
[/code]

Файл urls.py
 from django.urls import path
from .views import index, by_rubric
from .views import BbCreateView
urlpatterns = [
    path( '<int:rubric_id>/', by_rubric, name='by_rubric' ),
    path( 'add/', BbCreateView.as_view(), name='add' ),
    path( '', index, name='index' ),
]

Отредактировано 7emsk (Апрель 9, 2020 14:13:43)

Прикреплённый файлы:
attachment 78.png (180,0 KБ)

Офлайн

#2 Апрель 9, 2020 15:12:31

FishHook
От:
Зарегистрирован: 2011-01-08
Сообщения: 8179
Репутация: +  563  -
Профиль   Отправить e-mail  

Django PYTHON

Вам же написали чего не хватает и что нужно сделать



Офлайн

#3 Апрель 9, 2020 18:47:19

7emsk
Зарегистрирован: 2020-04-09
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Django PYTHON

FishHook
Вам же написали чего не хватает и что нужно сделать
Можно подробно или ссылку как это написать, а то начал изучать и куча не допонимании

Офлайн

#4 Апрель 9, 2020 19:20:03

FishHook
От:
Зарегистрирован: 2011-01-08
Сообщения: 8179
Репутация: +  563  -
Профиль   Отправить e-mail  

Django PYTHON

7emsk
Вероятно неправильным образом начали изучать, пройдите официальный туториал для начала, потом почитайте документацию по классам, которые используете. Я не знаю, что вам сказать больше чем дала подсказка Джанги. Вам написали "
Производные CreateView должны содержать атрибуты model или queryset, либо надо переопределить метод get_queryset.
Вот ваш класс

 class BbCreateView(CreateView):
    template_name = 'Spam/create.html'
    form_class = BbForm
    success_url = '/Spam/'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['rubrics'] = Rubric.objects.all()
        return context

должен создавать объект некой модели и записывать его в базу данных. Очевидно, что фреймворку недостаточно информации о том, инстанс какой именно модели надо создавать. И вам дали три пути как разрешить эту ситуацию. Что непонятно?



Офлайн

#5 Апрель 9, 2020 19:52:32

7emsk
Зарегистрирован: 2020-04-09
Сообщения: 3
Репутация: +  0  -
Профиль   Отправить e-mail  

Django PYTHON

FishHook
7emskВероятно неправильным образом начали изучать, пройдите официальный туториал для начала, потом почитайте документацию по классам, которые используете. Я не знаю, что вам сказать больше чем дала подсказка Джанги. Вам написали "Производные CreateView должны содержать атрибуты model или queryset, либо надо переопределить метод get_queryset. Вот ваш класс
Теперь другая ошибка

Прикреплённый файлы:
attachment 345678.png (129,2 KБ)

Офлайн

#6 Апрель 10, 2020 12:43:30

m0nte-cr1st0
Зарегистрирован: 2019-02-03
Сообщения: 49
Репутация: +  2  -
Профиль   Отправить e-mail  

Django PYTHON

7emsk
Теперь другая ошибка
иногда полезно показывать код.

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version