장고에서 양식과보기를 만들었습니다. http://localhost:8000/post/new/으로 갈 때 새 게시물 양식을 추가 할 수 있지만 모든 필수 필드를 완성한 후 제출하면 페이지가 새로 고쳐지고 게시 세부 정보 페이지로 리디렉션되지 않습니다.장고보기가 세부 정보 페이지로 리디렉션되지 않습니다.
여기 여기 내 urls.py의 내 views.py
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.createdAt = timezone.now()
post.writer = request.user
post.save()
return redirect('posts:post_detail', pk=post.pk)
else:
form = PostForm()
context = {'form':form}
return render(request,"posts/post_new.html",context)
입니다 :
<div class="col">
<form method='POST' class='post_form' enctype='multipart/form-data'>
{% csrf_token %}
{{ form.non_field_errors }}
<div class="form-row">
<div class="form-group col-md-6">
<label for="{{ form.title.id_for_label }}" class="col-form-label">Title</label>
<input type="text" class="form-control" id="{{ form.title.id_for_label }}" name= "{{ form.title.html_name }}" placeholder="Enter post title">
{{ form.title.errors }}
</div>
</div>
<div class="form-group">
<label for="{{ form.comment.id_for_label }}">Description here:</label>
<textarea class="form-control" rows="5" id="{{ form.comment.id_for_label }}" name="{{ form.comment.html_name }}" aria-describedby="descriptionHelpBlock"></textarea>
<small id="descriptionHelpBlock" class="form-text text-muted">
Describe your post in this text box.
</small>
{{ form.comment.errors }}
</div>
<div class="form-group">
<label for="{{ form.image.id_for_label }}">Upload picture here</label>
<input type="file" id="{{ form.image.id_for_label }}" name="{{ form.image.html_name }}" class="form-control-file">
{{ form.image.errors }}
</div>
<br>
<button type="submit" class="btn btn-info">Post</button>
</form>
이 게시물이 문제없이 만들어지고 post.slug로 슬러그 필드를 가정? –
불행히도 게시판이 db에 생성되지 않습니다 – MaxRah
게시물이 작성되지 않은 경우 양식이 유효하지 않은 것 같습니다. 'form.errors'를 완전히 인쇄하여 무엇이 잘못되었거나 누락되었는지 확인하는 것이 좋습니다. – Igor