2

I am trying to make a user not able to ask a Question only when he is logged in.

This is the error I'm getting in my terminal window:

    Internal Server Error: /ask_question/
    Traceback (most recent call last):
    File "/home/rayan/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
    File "/home/rayan/.local/lib/python3.8/site-packages/django/utils/deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
    File "/home/rayan/.local/lib/python3.8/site-packages/django/middleware/clickjacking.py",  line 26, in process_response
    if response.get('X-Frame-Options') is not None:
    AttributeError: 'function' object has no attribute 'get'
    [23/Jul/2020 17:19:16] "GET /ask_question/ HTTP/1.1" 500 62327

I am using a decorator, this is the code in my decorators.py:

def usersOnly(view_func):
    def func(request, *args, **kwargs):
        if request.user.is_authenticated:
            return view_func
        else:
            return redirect('login')
    return func

And this is the code in my views.py:

@usersOnly
def ask_question(request):
    form = AskQuestion()
    if request.method == "POST":
        asker = request.user
        form = AskQuestion(request.POST)
        if form.is_valid():
            form.save()
            id = form.cleaned_data.get('id')
            return redirect(f'/question/{id}/')
dic = {
    "form":form,
}
return render(request, 'blogs/ask_question.html', dic)

And this is the code from my models.py:

class Question(models.Model):
    title = models.CharField(max_length=250)
    asker = models.ForeignKey(User,on_delete=models.CASCADE)
    text = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.title

I am trying to allow only the logged in users to ask a question, if the user isn't logged in, he will be redirected to the login page. But it is giving me an error.

1 Answer 1

2

You need to call the function in the decorator, and return the result of the function, not the function itself:

def usersOnly(view_func):
    def func(request, *args, **kwargs):
        if request.user.is_authenticated:
            return view_func(request, *args, **kwargs)
        else:
            return redirect('login')
    return func

But what you here aim to do, already exists. You can make use of the @login_required decorator [Django-doc]:

from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy

@login_required(login_url=reverse_lazy('login'))
def ask_question(request):
    # …
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.