简体   繁体   中英

objects.all() not working - django

i have below model,

from django.db import models

# Create your models here.
class user_files(models.Model):
    Filename = models.CharField(max_length=50)
    Browse = models.FileField()

and in my view i want all data from above model, my view is..

def user_in(request):

    if not request.user.is_authenticated:
        return render(request, 'accounts/logout.html')

    else:
        if request.method == 'POST':
            form_new = Fileupload(request.POST, request.FILES )
            #instance=form_new.save(commit=False)
            #instance.save()
            if form_new.is_valid():
                form_new.save()
                return redirect('in')
        else:
            form_new = Fileupload()
            data = user_files.objects.all()
            return render(request, 'accounts/in.html', {'form_new': form_new}, {'data':data})

and in my template i am writing,

<div>
    {% if request.user.is_authenticated %}
        {% for da in data %}
            <h3>{{data.Filename}}</h3>
        {% endfor %}

    {% endif %}

</div>

but in my view, it is showing error for objects.all() as unresolved attribute. i am stuck . i am using pycharm. How to solve this?

Thanks in advance

You are rendering like this

<h3>{{data.Filename}}</h3>

'data' is a QuerySet it will not have any Filename attribute.

you have to do this.

<h3>{{da.Filename}}</h3>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM