简体   繁体   English

用Django查询不起作用

[英]Query with Django doesn't work

I am new to Django and Python. 我是Django和Python的新手。 This is my first post here!: 这是我第一次发帖!:

I have been trying to "print" on some website I made with Django some objects from a database that I made with HeidySQL. 我一直试图在我用Django创建的某些网站上“打印”一些我用HeidySQL创建的数据库中的对象。

The app inside Django is called users. Django中的应用称为用户。 So 127.0.0.1: 8000/users, inside this I have another page in 127.0.0.1:8000/users/login that is loading fine. 所以127.0.0.1:8000 / users,在其中我在127.0.0.1:8000/users/login中有另一个页面可以正常加载。

I successfully synced this DB with Django, I even managed to see some rows in the Django shell, which is why I concluded everything seems to be connected well. 我成功地将此数据库与Django进行了同步,甚至设法在Django shell中看到了一些行,这就是为什么我得出结论,一切似乎都连接良好的原因。

This login.html file atm has this: 这个login.html文件atm具有以下功能:

{% block content %}
<h1>{{ allcharacters_query }} </h1>

</body>

{% endblock %} 

The issue is that it doesn't display the names of my characters_list. 问题是它不显示我的character_list的名称。

Here is my views.py: 这是我的views.py:

from django.http import HttpResponse
from django.shortcuts import render
from users.models import Characters
from django.template.response import TemplateResponse

def index (request):
    return render(request,'users/home.html')

def return_charnames (request):
    allcharacters_query = Characters.objects.all()
    return render(request, 'users/login.html',{"allcharacters_query": 
    allcharacters_query})

And this my urls.py file: 这是我的urls.py文件:

from django.conf.urls import url, include, patterns
from .import views
from django.contrib.auth.views import login

urlpatterns = [
    url(r'^$', views.index,name='index'),
    url(r'^login/$',login,{'template_name': 'users/login.html' })

]

I don't really know how to fix this. 我真的不知道该如何解决。 I have read lots of documentation from the official Django site and watched lots of videos, however, I am stuck here. 我已经从Django官方网站上阅读了很多文档,并观看了很多视频,但是,我仍然在这里。 Thanks all! 谢谢大家!

如果要覆盖登录方法,则必须使用view render而不是django的视图进行登录。

 url(r'^login/$',views.return_charnames) 

Replace 更换

{% for lista in characters_list %}

by: 通过:

{% for lista in allcharacters_query %}

characters_list doesn't exist in your template. 模板中不包含characters_list

And there is no URL calling your view return_charnames in your url file. 在URL文件中,没有URL调用您的视图return_charnames

您应该将return_charnames添加到urls.py中,以正确的url显示html内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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