“Django Créer une vue” Réponses codées

createEview django

# Required imports
from django.urls import reverse_lazy

class YourView(CreateView):
	model 			= Model
    fields 			= ['your_fields']
    template_name 	= 'your_template'
    success_url		= reverse_lazy('home')
    
    def form_valid(self, form):
        form.instance.user = self.request.user
        super(YourView, self).form_valid(form)
        return redirect('home')
Virgin Programmer

vue django

from django.http import HttpResponse #last line below allows MyView.get 
# to return an HttpResponse object 
from django.views import View #View is to become the parent class of MyView

class MyView(View):
	''' View is the parent class that provides method as_view() to MyView '''
    def get(self, request, *args, **kwargs):
        return HttpResponse('Hello, World!')
ruperto2770

Django Créer une vue

class AuthorCreateView(CreateView):
    form_class = AuthorForm
    template_name = 'author_new.html'
    success_url = 'success'
omeatai

Django Créer une classe de vue

###### views.py #####
from .forms import  CreateArticleForm
from django.views.generic import CreateView
class ArticleCreateView(CreateView):
    form_class = CreateArticleForm
    template_name = 'articles/create_article.html'
    
###### urls.py  ######
from .views import ArticleCreateView
urlpatterns =[ path('articles/create/', ArticleCreateView.as_view()),]
PersianBuddy

Réponses similaires à “Django Créer une vue”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code