Python Can';t在django应用程序中从MongoDB检索数据
我想在Django开发一个网站。我第一次使用MongoDB作为数据库。我使用Djongo开发django和MongoDB之间的连接。我在Robo3T中创建了一个数据库,django和MongoDB之间也建立了连接。数据很好地保存在我在robo3T中看到的数据库中。但是,当我试图从MongoDB检索数据到Django模板上的dispay时,并不是像这样显示数据和模板标记{{post.tile}{{post.content}。 这是我的密码: Setting.pyPython Can';t在django应用程序中从MongoDB检索数据,python,django,mongodb,Python,Django,Mongodb,我想在Django开发一个网站。我第一次使用MongoDB作为数据库。我使用Djongo开发django和MongoDB之间的连接。我在Robo3T中创建了一个数据库,django和MongoDB之间也建立了连接。数据很好地保存在我在robo3T中看到的数据库中。但是,当我试图从MongoDB检索数据到Django模板上的dispay时,并不是像这样显示数据和模板标记{{post.tile}{{post.content}。 这是我的密码: Setting.py DATABASES = {
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'test',
'HOST':'localhost',
'PORT':27017
}
}
view.py
from django.shortcuts import render
from django.views.generic import View,TemplateView
from .forms import FormName
from .models import Post
# Create your views here.
class MainPageView(TemplateView):
template_name='main.html'
class LoginView(TemplateView):
template_name='login.html'
def RegistrationView(request):
form=FormName()
if request.method == 'POST':
print("In Post")
print(request.POST)
form=FormName(request.POST)
if form.is_valid():
form.save(commit=True)
else:
form=FormName()
return render(request,'registration.html',{'form':form})
# template_name='registration.html'
def main(request):
post=Post.objects.all()
return render(request,'main_temp.html',{'Post':post})
models.py
from django.db import models
# from OnlineJobPortal.settings import DBNAME
# # Create your models here.
# # connect(DBNAME)
class Post(models.Model):
title=models.CharField(max_length=250)
content=models.CharField(max_length=250)
forms.py
from django import forms
from .models import Post
class FormName(forms.ModelForm):
class Meta():
model=Post
fields='__all__'
将模板标记更改为
{{post.tile}}
等。
空格起作用,因为如果没有空格,模板标记将无法正确解释
您在访问模板中的字段(
post.title
而不是post.tile
)时犯了拼写错误。您的模板编码结构应该与以下类似
{% for post in Post %}
<span>{{ post.title }}</span>
<span>{{ post.content }}</span>
{% endfor %}
{%用于post%中的post}
{{post.title}}
{{post.content}}
{%endfor%}