작성한 모델과 로그인 부분입니다....
관심을 가져주셔서 감사합니다.
아래에 모델과 뷰, 로그인 html의 코드가 있습니다.
이해가 안되는 부분이 많아 안되는 부분을 잡아내지 못하네요.
model.py
from django.db import models
from django.contrib.auth.models import User
Create your models here.
class Question(models.Model):
author=models.ForeignKey(User, on_delete=models.CASCADE, null=True)
subject=models.CharField(max_length=200)
content=models.TextField()
create_date = models.DateTimeField()
modify_date=models.DateTimeField(null=True,blank=True)
class Answer(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
question=models.ForeignKey(Question, on_delete=models.CASCADE)
content=models.TextField()
create_date = models.DateTimeField()
modify_date = models.DateTimeField(null=True, blank=True)
pybo/templates/login.html
{% extends 'base.html' %}
{% block content %}
LogIn
create an account
{% csrf_token %} {% include "form_errors.html" %}
User ID
{{form.username.value|default_if_none:''}}
PassWord
••••••••••••••••••••••••••••••••••••••••••
LogIn
{% endblock %}
pybo/views.py
from django.shortcuts import render, get_object_or_404, redirect
Create your views here.
from django.http import HttpResponse
from .models import Question
from django.utils import timezone
from .forms import QuestionForm,AnswerForm
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
from django.contrib import messages
def index(request):
"""
pybo list
question_list=Question.objects.order_by('-create_date')
context={'question_list':question_list}
"""
page=request.GET.get('page','1')
question_list=Question.objects.order_by('-create_date')
paginator=Paginator(question_list,10)
page_obj=paginator.get_page(page)
#context={'question_list':question_list}
context={'question_list': page_obj}
return HttpResponse(" Welcome to Pybo")
return render(request,'pybo/question_list.html',context)
def detail(request,question_id) :
question = get_object_or_404(Question, pk=question_id)
context={'question':question}
return render(request,'pybo/question_detail.html', context)
@login_required(login_url='common:login')
def answer_create(request,question_id):
question=get_object_or_404(Question, pk=question_id)
question.answer_set.create(content=request.POST.get('content'),
create_date=timezone.now())
return redirect('pybo:detail', question_id=question.id)
#return redirect('pybo:detail',question_id=question.id)
@login_required(login_url='common:login')
def question_create(request):
if request.method =='POST':
form=QuestionForm(request.POST)
if form.is_valid():
question=form.save(commit=False)
question.author=request.user
question.create_date=timezone.now()
question.save()
return redirect('pybo:index')
else:
form=QuestionForm()
context={'form':form}
return render(request,'pybo/question_form.html', context)
@login_required(login_url='common:login')
def answer_create(request,question_id):
question=get_object_or_404(Question, pk=question_id)
if request.method =='POST':
form=AnswerForm(request.POST)
if form.is_valid():
answer=form.save(commit=False)
answer.author = request.user
answer.question = question
answer.create_date=timezone.now()
answer.save()
return redirect('pybo:detail',question_id=question.id)
else:
form=AnswerForm()
context={'question':question,'form':form}
return render(request,'pybo/question_detail.html', context)
@login_required(login_url='common:login')
def question_modify(request,question_id):
question=get_object_or_404(Question,pk=question_id)
if request.user !=question.auther:
messages.error(request,'You have no permission')
return redirect('pybo:detail',question_id=question.id)
if request.method=="POST":
form=QuestionForm(request.POST,instance=question)
if form.is_valid():
question=form.save(commit=False)
question.auther=request.user
question.modify_date=timezone.now()
question.save()
return redirect('pybo:detail',question_id=question.id)
else:
form=QuestionForm(instance=question)
context={'form':form}
return render(request,'pybo/question_form.html',context)
@login_required(login_url='common:login')
def question_delete(request,question_id):
question=get_object_or_404(Question,pk=question_id)
if request.user !=question.author:
messages.error(request,'have no permission to delete')
return redirect('pybo:detail',question_id=question.id)
question.delete()
return redirect('pybo:index')
최원호 님 404
M 2021년 3월 23일 6:36 오후
어디가 어떻게 잘못된것인지 모를때에는 책의 원본소스와 비교해 보시는게 제일 좋습니다. 책의 원본소스는 깃허브에 올라가 있구요. 깃허브에 있는 소스가 잘 동작하는지 확인해 보시고 본인이 작성한 코드와 어떤점이 다른지 확인해 보세요.
-
박응용님,
2021년 3월 23일 6:48 오후
추천
,
대댓글