html 中的 django 模型数据 returns none 但显示在管理面板中
django model data returns none in html but shows in admin panel
我创建了一个模型,让用户可以创建一个简单的 post,但是当提交数据时,它会在管理面板中注册,但不会显示在 html 中。我可以在管理员中编辑,甚至可以在文件夹中编辑图像,不幸的是我无法找出问题所在。
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'),
]
模板文件:
<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>
我没有收到任何错误,但不确定是什么问题。
如果你看对了,错误就出在 posts
视图本身。
一定是{'userpost': userpost}
不是{'userpost': Posts}
.
试试这个:
views.py
def posts(request):
userpost = Posts.objects.all()
return render(request, 'posts.html', {'userpost': userpost})
然后,你应该按以下方式循环你的 userpost
,这只是一个最小的可复制示例,因为我无法理解设计但它应该显示你的数据:
模板文件:
<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.
我创建了一个模型,让用户可以创建一个简单的 post,但是当提交数据时,它会在管理面板中注册,但不会显示在 html 中。我可以在管理员中编辑,甚至可以在文件夹中编辑图像,不幸的是我无法找出问题所在。
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'),
]
模板文件:
<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>
我没有收到任何错误,但不确定是什么问题。
如果你看对了,错误就出在 posts
视图本身。
一定是{'userpost': userpost}
不是.{'userpost': Posts}
试试这个:
views.py
def posts(request):
userpost = Posts.objects.all()
return render(request, 'posts.html', {'userpost': userpost})
然后,你应该按以下方式循环你的 userpost
,这只是一个最小的可复制示例,因为我无法理解设计但它应该显示你的数据:
模板文件:
<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 onlyPost
instead of, asPosts
s
is by default added in models as the suffix.