Django: Как добавить DeleteView и UpdateView
Привет я студент в django, я хочу добавить UpdateView и DeleteView, может кто-нибудь из экспертов может помочь мне разобраться (сайт форума) :D
я предоставляю некоторые из моих кодов views.py
class TopicView(View):
def get(self, request, *args, **kwargs):
tid = request.GET.get('id')
print(tid)
topic = Topic.objects.filter(id=tid).first()
print(topic)
# topic.clicks += 1
topic.save()
username = check_result(request, topic)
print(username)
comments = topic.comments()
content = create_content_paginator(request, queryset=comments, page_size=5)
content.topic = topic
return render(
request,
'topic.html',
context={'username': username, 'content': content, 'form': Form}
)
@check_login
def post(self, request, *args, **kwargs):
topic_id = request.POST.get('topic', '')
content = request.POST.get('content')
topic = Topic.objects.filter(id=topic_id).first()
username = check_result(request, topic)
if content:
user = UserInfo.objects.filter(username=username).first()
comment = Comment(user=user, topic_id=topic_id, content=content)
comment.save()
comments = topic.comments()
content = create_content_paginator(request, queryset=comments, page_size=5)
content.topic = topic
print(Form.errors)
return render(
request,
'topic.html',
context={'username': username, 'content': content, 'form': Form}
)
tid
= Topic_id post_id ...
content
= содержание внутри темы
class PublicView(View):
@check_login
def get(self, request, pid, *args, **kwargs):
subject = Subject.objects.filter(id=pid).first()
# semester = subject.semester
username = request.session.get('name')
return render(
request,
'public.html',
context={'username': username, 'form': Form, 'content': {'subject': subject}}
)
@check_login
def post(self, request, pid, *args, **kwargs):
subject = Subject.objects.filter(id=pid).first()
username = request.session.get('name')
print(username,'asdasd')
user = UserInfo.objects.filter(username=username).first()
name = request.POST.get('title')
content = request.POST.get('content')
semester_id = request.POST.get('semester_id')
if pid and name and content and semester_id:
try:
topic = Topic(name=name, content=content, subject_id=pid,user=user)
topic.save()
red = redirect(f'/topic/?id={topic.id}')
except Exception as E:
print(E)
red = redirect(f'/404/')
return red
return render(
request,
'public.html',
context={'username': username, 'form': Form, 'content': {'subject': subject}}
)
Чтобы использовать общие представления, такие как CreateView, UpdateView и т.д.. Вам просто нужно сделать следующее:
class TopicView(UpdateView):
model = #enter the name of the model in which you have to make updation.
fields = #enter the name of the fields in which you have to display in update
form. Eg:fields=['name','place']
template_name = #enter the template name in that you are going to display the
update form