HttpRequestResponse重新加载到django 2.0.3中的同一页面

时间:2018-03-20 10:39:38

标签: python django

1.我正在尝试使用django 2.0.3开发一个简单的博客。在view.py当我试图从edit.html或create.html重定向到页面时,当我手动提供网址时,它会重定向到它自己仍在加载到同一页面,我不知道为什么。

views.py

{"name": "oscar", "friend": {"name": "john", "friend": {"name": "oscar", ...

2.this my urls.py

urls.py

from django.shortcuts import render,get_object_or_404,redirect
from django.http import HttpResponseRedirect,HttpResponse
from posts.models import Post
from posts.forms import PostForm
from django.contrib import messages
from django.urls import reverse
def post_create(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        HttpResponseRedirect(instance.get_absolute_url())
    context = {"title":"Create Blog","form":form}
    return render(request,"create.html",context)

def post_detail(request,id):
    instance = get_object_or_404(Post,id=id) 
    context = {
    "title": instance.title,
    "instance":instance
    }
    return render(request,"detail.html",context)


def post_update(request,id):
    instance = get_object_or_404(Post,id=id)
    form = PostForm(request.POST or None,instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request,"Saved")
        HttpResponseRedirect(instance.get_absolute_url())
    context = {
    "title": "Edit Blog",
    "instance":instance,
    "form":form
    }
    return render(request,"edit.html",context)


def post_delete(request):
    context = {"title":"Delete"}
    return HttpResponse("<h1>delete</h1>")

def post_list(request):
    queryset = Post.objects.all().order_by('-timestamp')
    context = {
    "title":"List",
    "object_list":queryset
    }
    return render(request,"index.html",context)             

3.this my models.py

model.py

from django.contrib import admin
from django.urls import path,include,re_path
from . import views
app_name = 'posts'
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^$',views.post_list,name="list"),
    re_path(r'^create/$',views.post_create,name="create"),
    re_path(r'^(?P<id>\d+)/$',views.post_detail,name="detail"),
    re_path(r'^(?P<id>\d+)/edit/$',views.post_update,name="update"),
    re_path(r'^delete$',views.post_delete),



]

0 个答案:

没有答案