重定向到所需的页面但url在django中没有变化

时间:2018-01-21 15:41:46

标签: python django django-models django-templates django-views

我创建了一个小型django项目。当我尝试重定向到特定页面时,该URL不会更改,但所需页面正在正确加载。我正在使用render()重定向到特定的页面。

views.py文件:

    def create_album(request):
    if not request.user.is_authenticated():
        return render(request, 'photo/login.html')
    else:
        form = AlbumForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            album = form.save(commit=False)
            album.user = request.user
            album.album_logo = request.FILES['album_logo']
            album.save()
            return render(request, 'photo/detail.html', {'album': album})
        context = {
            "form": form,
        }
        return render(request, 'photo/create_album.html', context)

def register(request):
    form = UserForm(request.POST or None)
    if form.is_valid():
        user = form.save(commit=False)
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                albums = Album.objects.filter(user=request.user)
                return render(request, 'photo/index.html', {'albums': albums})
    context = {
        "form": form,
    }
    return render(request, 'photo/register.html', context)
    def detail(request, album_id):
        if not request.user.is_authenticated():
            return render(request, 'photo/login.html')
        else:
            user = request.user
            album = get_object_or_404(Album, pk=album_id)
            return render(request, 'photo/detail.html', {'album': album, 'user': user})

urls.py文件:

    from django.conf.urls import url
from . import views

app_name = 'photo'

urlpatterns = [
    url(r'^login_user/$', views.login_user, name='login_user'),
    url(r'^register/$', views.register, name='register'),
    url(r'^logout_user/$', views.logout_user, name='logout_user'),
    url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^create_album/$', views.create_album, name='create_album'),
    url(r'^(?P<album_id>[0-9]+)/create_picture/$', views.create_picture, name='create_picture'),
    url(r'^(?P<album_id>[0-9]+)/delete_album/$', views.delete_album, name='delete_album'),
    url(r'^(?P<song_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),
    url(r'^(?P<album_id>[0-9]+)/favorite_album/$', views.favorite_album, name='favorite_album'),
    url(r'^(?P<album_id>[0-9]+)/delete_pic/(?P<song_id>[0-9]+)/$', views.delete_pic, name='delete_pic'),
    url(r'^$', views.index, name='index'),
    ]

问题是当我尝试创建一个专辑时,我得到的URL为:127.0.0.1:8000 / create_album / 创建相册后,页面必须重定向到detail.html页面。该网址应为:127.0.0.1:8000/10 /(10为album_id)

但网址没有变化。请帮助我。

1 个答案:

答案 0 :(得分:2)

你误解了renderrender,正如其名称,只是呈现html页面。即使您呈现detail.html页面,此处的请求上下文仍然是create_album请求。

您需要redirecthttps://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#redirect):

def create_album(request):
    if not request.user.is_authenticated():
        return redirect('photo:login_user')
    else:
        form = AlbumForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            album = form.save(commit=False)
            album.user = request.user
            album.album_logo = request.FILES['album_logo']
            album.save()
            return redirect('photo:detail', album_id=album.id)

        context = {
            "form": form,
        }
        return render(request, 'photo/create_album.html', context)