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.