如果request.user.is_anonymous:-其他:运行不正常

时间:2018-07-25 14:11:24

标签: python django

我正在DB中创建一个具有2种不同条件的行,当没有用户登录时,以及当用户登录时

我用过

if request.user.is_anonymous:
 [....]
else:
 [....]

但是它仅在第一行为if request.user.is_anonymous:时才保存到DB,但是在用户登录时从不保存到DB。

def add_ad_mod(request):
    created_otp = uuid.uuid4().hex[:6].upper()
    current_user = request.user
    current_ip = get_client_ip(request)
    selected = Temp.objects.filter(created_by_ip=current_ip).order_by('-created_at')[0]
    selected_category = selected.cat
    classi = Category.objects.filter(category_name__icontains="Classifieds")[0]
    jobs = Category.objects.filter(category_name__icontains="Jobs")[0]
    if request.user.is_anonymous:
        if request.method == 'POST':
            add_ad_mod_form = AddAdModForm(request.POST, request.FILES, cat=selected_category)
            if add_ad_mod_form.is_valid():
                model_instance = add_ad_mod_form.save(commit=False)
                model_instance.category = selected_category
                model_instance.post_otp = created_otp
                model_instance.save()
                add_ad_mod_form.save_m2m()
                current_email = model_instance.email
                send_mail(
                    'Activate your Ad on Jehlum',
                    'Use the code ' + created_otp + '.',
                    'test@jehlem.org',
                    [model_instance.email],
                    fail_silently=False,
                )
                request.session['created_otp'] = created_otp  # set 'student_id' in the session
                request.session['current_email'] = current_email  # set 'student_id' in the session
            return redirect('post_confirm')
        else:
            add_ad_mod_form = AddAdModForm(cat=selected_category)
    else:
        if request.method == 'POST':
            add_ad_mod_form = AddAdModForm(request.POST, request.FILES, cat=selected_category)
            if add_ad_mod_form.is_valid():
                model_instance = add_ad_mod_form.save(commit=False)
                model_instance.created_by = current_user.email
                model_instance.category = selected_category
                model_instance.email = current_user.email
                model_instance.post_otp = 0
                if request.user.is_superuser:
                    model_instance.is_active = True
                else:
                    model_instance.is_active = False
                model_instance.save()
                add_ad_mod_form.save_m2m()
            return redirect('post_success')
        else:
            add_ad_mod_form = AddAdModForm(cat=selected_category)

    add_ad_mod_form = AddAdModForm(cat=selected_category)

    context = {
        'add_ad_mod_form': add_ad_mod_form,
        'selected_category': selected_category,
        'classi': classi,
        'jobs': jobs,
    }
    return render(request, 'add_ad_mod.html', context)

2 个答案:

答案 0 :(得分:3)

is_anonymous是一种方法,因此必须调用:

if request.user.is_anonymous():  # note parentheses
    # ...

您通常会在django 模板中看到不带括号的版本,其中渲染引擎将隐式调用该函数。但是,在常见的Python代码中,user.is_anonymous引用的函数对象始终是真实的,而不管对该函数的调用将返回什么。

答案 1 :(得分:0)

自Django 1.10 is_anonymous成为布尔对象以来,不再是方法。在旧版本中,这是一种方法。在Django 2.0中已删除。

Documentation

中已明确指出

是匿名用户:只读属性,始终为False。这是区分User和AnonymousUser对象的一种方式。通常,您应该更喜欢使用is_authenticated来使用此属性。

使用is_anonymous代替is_authenticated

if request.user.is_authenticated:
    # authenticated user stuff
else:
    # Anonymous user