简体   繁体   中英

django model data returns none in html but shows in admin panel

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 class="container mt-5">
    <div class="row">
    <div class="col-sm-6">
      <div class="card-header">
        {{ userpost.quote }}
        <br>
        <div class="img">
          <a href=""><img src="{{ userpost.image.url 
       }}"></a>
 
        </div>
      <div class="card-body">
  
      </div>
      </div>
       
     </div>
     <div class="col-sm-6">
      <em>
        {{ userpost.date }}
      </em>     

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

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 class="container mt-5">
    <div class="row">
    <div class="col-sm-6">
        {% for single_post in userpost %}
        <div class="card-header">
        {{ single_post.quote }}
        <br>
        <div class="img">
            <a href=""><img src="{{single_post.image.url 
        }}"></a>
    
        </div>
        <div class="card-body">
    
        </div>
        </div>
        
        </div>
        <div class="col-sm-6">
        <em>
        {{ single_post.date }}
        </em>
        {% endfor %}

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

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