Ahsan Shuvo
Ahsan Shuvo

Reputation: 5

'NewPost 'Object has no attribute 'user'(closed)

In my forum app,I want to show post details with it's author name. In 'new-post' class in views.py I tried to save the author name by post.user = self.request.user But whenever my current login user submits his new post it gives the mentioned error.

views.py:

class NewPost(CreateView):
    form_class = PostForm
    template_name = 'post_form.html'

@login_required
def form_valid(self,form):
    post = form.save(commit=False)
    post.user= self.request.user
    post.save()
    return redirect('website:details', post=post)

forms.py:

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields=('title','description')

Models.py:

class Post(models.Model):
    user =  models.ForeignKey(User, on_delete =models.CASCADE)
    title = models.CharField(max_length = 500, blank = False)
    description = models.TextField()

def __str__(self):
    return self.title

html file:

<div class ="container-fluid">
<form method="post">
    {% csrf_token %}

    {{ form.as_p }}
    <input type="submit" value="Save">
</form>
</div>

AttributeError at /website/add/ <br>
'NewPost' object has no attribute 'user'<br>
Request Method: POST <br>
Request URL:    http://127.0.0.1:8000/website/add/<br>
Django Version: 1.9 <br>
Exception Type: AttributeError <br>
Exception Value:    <br>
'NewPost' object has no attribute 'user' <br>
Exception Location: C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\contrib\auth\decorators.py in _wrapped_view, line 22 <br>
Python Executable:  C:\Python27\python.exe<br>
Python Version: 2.7.14 <br>
Python Path:    <br>
['C:\\Users\\as_couple\\Desktop\\STUDENTTRACKERSYSTEM',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\Python27\\DLLs',<br>
 'C:\\Python27\\lib',<br>
 'C:\\Python27\\lib\\plat-win',<br>
 'C:\\Python27\\lib\\lib-tk',<br>
 'C:\\Python27', <br>
 'C:\\Python27\\lib\\site-packages', <br>
 'C:\\Python27\\lib\\site-packages\\django_admin-1.1.1-py2.7.egg', <br>
 'C:\\Python27\\lib\\site-packages\\django_excel_response2-2.0.8-py2.7.egg',<br>
 'C:\\Python27\\lib\\site-packages\\django_six-1.0.4-py2.7.egg',<br>
 'C:\\Python27\\lib\\site-packages\\django-1.9-py2.7.egg']<br>

Updated file:

Traceback:`File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\core\handlers\base.py" in get_response 149. response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\core\handlers\base.py" in get_response 147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\views\generic\edit.py" in post 255. return super(BaseCreateView, self).post(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\views\generic\edit.py" in post 221. return self.form_valid(form)

File "C:\Python27\lib\site-packages\django-1.9-py2.7.egg\django\contrib\auth\decorators.py" in _wrapped_view 22. if test_func(request.user):

Exception Type: AttributeError at /website/add/ Exception Value: 'NewPost' object has no attribute 'user'`

Upvotes: 0

Views: 460

Answers (2)

Fran
Fran

Reputation: 899

I supposed that

class NewPost(CreateView):
    form_class = PostForm
    template_name = 'post_form.html'

@login_required
def form_valid(self,form):
    post = form.save(commit=False)
    post.user= self.request.user
    post.save()
    return redirect('website:details', post=post)

is unindented, and that def form_valid is a method of your class. When using Class Based View you cannot decorate methods directly. Instead, you need to decorate the dispatch, like this

class NewPost(CreateView):
    form_class = PostForm
    template_name = 'post_form.html'

def form_valid(self,form):
    post = form.save(commit=False)
    post.user= self.request.user
    post.save()
    return redirect('website:details', post=post)

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
    return super(NewPost, self).dispatch(*args, **kwargs)

i.e. the error is not fired in your form_valid, but in the decorator code

Upvotes: 1

Ngoc Pham
Ngoc Pham

Reputation: 1450

Try add user in class Meta. Like fields=('title','description','user')

Upvotes: -1

Related Questions