我在登录页面重定向,每次点击主页或尝试登录我都会被重定向到登录

时间:2017-03-27 10:15:59

标签: python django

当我打开系统时,我希望被重定向到主页,如何编码我的middleware.py以将我重定向到家或在用户打开系统时将用户重定向到主页。谢谢你 这是来自开发服务器的响应

[27/Mar/2017 12:59:28] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:28] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:29] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:29] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:29] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:29] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:29] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:29] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:30] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:30] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:30] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:30] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:30] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:30] "GET /account/login/ HTTP/1.1" 200 5858

[27/Mar/2017 12:59:30] "GET / HTTP/1.1" 302 0
account/login/
[27/Mar/2017 12:59:30] "GET /account/login/ HTTP/1.1" 200 5858

这是我的middleware.py

import re
from django.conf import settings
from django.shortcuts import redirect

EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]


class LoginRequiredMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response
    def process_view(self, request, view_func, view_args, view_kward):
        assert hasattr(request, 'user')
        path = request.path_info.lstrip('/')
        print(path)

        if not request.user.is_authenticated():
            if not any(url.match(path) for url in EXEMPT_URLS):
                return redirect(settings.LOGIN_URL)

这是我的settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
LOGIN_REDIRECT_URL = '/account/'

LOGIN_URL = '/account/login/'
LOGIN_EXEMPT_URL = (
    r'^account/logout/$',
    r'^account/register/$'
    )

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025

这是我的view.py

from django.contrib.auth.decorators import login_required
from blog.forms import (
    RegistrationForm,
    EditProfileForm
    )
from django.shortcuts import render,redirect
from django.contrib.auth import update_session_auth_hash
from django.utils import timezone
from .models import Post, Comment
from django .shortcuts import render, get_object_or_404
from .forms import PostForm, CommentForm
from django.shortcuts import redirect,HttpResponse
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm, PasswordChangeForm


def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    """posts = Post.objects.filter(published_date__lte=timezone.now()).get_latest_by('published_date')"""
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})
@login_required
def post_new(request):
    form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})
@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})
@login_required
def post_draft_list(request):
    posts = Post.objects.filter(published_date__isnull=True).order_by('created_date')
    return render(request, 'blog/post_draft_list.html', {'posts': posts})
@login_required
def post_publish(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.publish()
    return redirect('post_detail', pk=pk)
@login_required
def publish(self):
    self.published_date = timezone.now()
    self.save()
@login_required
def post_remove(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.delete()
    return redirect('post_list')
@login_required
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'blog/post_edit.html', {'form': form})
@login_required
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/add_comment_to_post.html', {'form': form})
@login_required
def comment_approve(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    comment.approve()
    return redirect('post_detail', pk=comment.post.pk)

@login_required
def comment_remove(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post_pk = comment.post.pk
    comment.delete()
    return redirect('post_detail', pk=post_pk)
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()

            return redirect('/register/')

    else:
        form = RegistrationForm()
        args = {'form': form}
        return render(request, 'registration/register.html', args)
@login_required
def view_profile(request):
    args = {'user': request.user}
    return render(request, 'registration/profile.html', args)
@login_required
def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect('/profile/')
    else:
        form = EditProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'registration/edit_profile.html', args)  

@login_required
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/account/profile/password/')
        else:
            return redirect('account/change_password')
    else:
        form = PasswordChangeForm(user=request.user)

        args = {'form': form}
        return render(request, 'registration/change_password.html', args)

0 个答案:

没有答案