Home > Back-end >  django model data returns none in html but shows in admin panel
django model data returns none in html but shows in admin panel

Time:05-26

I've created a model that lets users create a simple post however when the data is submitted it registers in the admin panel but will not display in the html. I can edit in admin and the images even up in the folder unfortunately I cannot find out what the issue is.

models.py


from distutils.command.upload import upload
from django.db import models
from django.forms import CharField, ImageField
from django.forms.fields import DateTimeField
from matplotlib import image
from sqlalchemy import true
from django.contrib import admin

class Posts(models.Model):
    image = models.ImageField(upload_to='images/')
    quote = models.TextField(null=True)
    date = models.DateTimeField(auto_now_add=True, 
    null=True)

views.py


from django.contrib.auth import login, authenticate
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from requests import request
from users.forms import SignUpForm
from matplotlib.style import context
from django.http import HttpRequest, HttpResponse

from .models import Posts

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('profile')  #profile
    else:
        form = SignUpForm() #was UserCreationForm
    return render(request, 'signup.html', {'form': form})

def posts(request):
    userpost = Posts.objects.all()
    return render(request, 'posts.html', {'userpost': Posts} )

urls.py

from django.urls import path
from django import urls
from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView

urlpatterns = [
    path('login/', LoginView, {'template_name': 'users/login.html'}, name = 'login'),
    url('signup/',views.signup, name = 'signup'),
    url('posts/',views.posts, name = 'posts'),

]

Template file:

<div >
    <div >
    <div >
      <div >
        {{ userpost.quote }}
        <br>
        <div >
          <a href=""><img src="{{ userpost.image.url 
       }}"></a>
 
        </div>
      <div >
  
      </div>
      </div>
       
     </div>
     <div >
      <em>
        {{ userpost.date }}
      </em>     

I receive no errors but am unsure as to what the issue is.

CodePudding user response:

If you look at it correctly the mistake is in the posts view itself.

It must be {'userpost': userpost} not {'userpost': Posts} .

Try this:

views.py

def posts(request):
    userpost = Posts.objects.all()
    return render(request, 'posts.html', {'userpost': userpost})

Then, you should loop your userpost in following way, its only a minimal reproductible example, as I am not able to understand the design but it should display your data:

Template file:

<div >
    <div >
    <div >
        {% for single_post in userpost %}
        <div >
        {{ single_post.quote }}
        <br>
        <div >
            <a href=""><img src="{{single_post.image.url 
        }}"></a>
    
        </div>
        <div >
    
        </div>
        </div>
        
        </div>
        <div >
        <em>
        {{ single_post.date }}
        </em>
        {% endfor %}

Note: Models in django are generally in singular form, It will be better if you name it as Post instead of Posts, as s is by default added as the suffix.

  • Related