简体   繁体   English

Django:我无法在网站上显示从数据库中搜索到的信息

[英]Django: I can not display the information searched from the database on the website

I'm setting up a user registration / login / search program and when I try to find users by email and show them on the site it's like they do not find it in the database, but it's there. 我正在设置用户注册/登录/搜索程序,当我尝试通过电子邮件查找用户并将其显示在网站上时,就像他们在数据库中找不到该用户一样,但是它在那里。

views.py views.py

from django.shortcuts import render 
from django.views.generic import CreateView, TemplateView
from django.http import HttpResponseRedirect
from django.contrib.auth.views import login, logout     
from django.core.urlresolvers import reverse_lazy, reverse
from .forms import CustomUserCreationForm
from .models import MyUser


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

def login_view(request, *args, **kwargs):   
    if request.user.is_authenticated():    
         return HttpResponseRedirect(reverse('usuarios:home'))

    kwargs['template_name'] = 'usuarios/login.html'             
    kwargs['extra_context'] = {'next': reverse('usuarios:home')} 
    return login(request, *args, **kwargs)                    

def logout_view(request, *args, **kwargs):  
    kwargs['next_page'] = reverse('usuarios:home') 
    return logout(request, *args, **kwargs)

class SerchView(TemplateView):
    template_name = "usuarios/pesquisar.html" 

    def search_view(request, **kwargs): 
        email = MyUser(email = request.GET['email'], nome = request.GET['nome'])
        myusers = MyUser.objects.filter(email__contains = 'email')  
        context = {'myusers': myusers}
        kwargs['extra_context'] = {'next': reverse('usuarios:pesquisar')}
        print (request.GET) 
        return render(request, context, **kwargs)

class RegistrationView(CreateView): 
    form_class = CustomUserCreationForm     
    success_url = reverse_lazy('usuarios:login')
    template_name = "usuarios/registrar.html"  

models.py models.py

from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager                                     
from django.contrib.auth.models import PermissionsMixin               

class EmailUserManager(BaseUserManager):    

    def create_user(self, *args, **kwargs):
        email = kwargs["email"]
        email = self.normalize_email(email)
        password = kwargs["password"]
        kwargs.pop("password")

        if not email:   
            raise ValueError(('Usuário deve inserir um endereço de e-mail.'))

        user = self.model(**kwargs)
        user.set_password(password)
        user.save(using = self._db)
        return user    

    def create_superuser(self, *args, **kwargs):     
        user = self.create_user(**kwargs)
        user.is_superuser = True
        user.save(using = self._db)
        return user

class MyUser(PermissionsMixin, AbstractBaseUser):   

    email = models.EmailField(
        verbose_name = ('Endereço de E-mail'),
        unique = True,
    )

    nome = models.CharField(            
        verbose_name = ('Nome'),
        max_length = 50,
        blank = False,  #Não permite um valor 'vazio'
        help_text = ('Informe seu nome completo.'),     
    )   

    USERNAME_FIELD = 'email'
    objects = EmailUserManager()

pesquisar.html pesquisar.html

<h1>Busca de Usuários</h1>

<html>
<body>
       <form role = "form" class = "form" method = "GET" action = "{{ request.path }}{% if next %}?next={{ next }}{% endif %}">
              {% csrf_token %}
              <h5></h5>       
              <label for="email">E-mail <input type="text" name="email"></label>
              <input type = "submit" value='Pesquisar' />
              <h5></h5>
       </form>
       <table border="1">                
              <thead>
                     <th>Nome</th>
                     <th>E-mail</th>
              </thead>                
              <tbody>
                       {% for myuser in myusers %}     
                                   <tr>
                                   <td> {{ myuser.nome }} {{ myuser.email }}</td>
                                   </tr>
                       {% endfor %}
              </tbody>       
       </table>
</body>
</html>

I can not figure out what I'm doing wrong. 我不知道我在做什么错。 Can someone point me in the right direction? 有人可以指出我正确的方向吗? Thank you in advance! 先感谢您!

This part of the code makes no sense: 这部分代码没有意义:

    email = MyUser(email = request.GET['email'], nome = request.GET['nome'])
    myusers = MyUser.objects.filter(email__contains = 'email')  

maybe you meant something like: 也许你的意思是这样的:

myusers = MyUser.objects.filter(email__contains = request.GET['email'])  

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

相关问题 Django:如何根据表单中的选定选项在模板上显示来自 django 数据库的图像? - Django: how can i display an image from the django database on a template based on an selected option from a form? Flask 和 Python - 单击按钮时如何显示包含数据库信息的卡片 - Flask with Python - How can I display a card with information from database when a button is clicked 我无法从网站下载我需要的信息 - I can't download the information I need from the website 如何显示 SQLite 数据库中的某些信息? - How do I display certain information from SQLite Database? 如何使用我从网站上抓取的json填充django数据库 - How to populate my django database with json that I scraped from a website 我无法使用 Python 和 Django 在 web 页面上显示数据库中的数据 - I can't display data from database on web page using Python and Django 如何将项目中的信息传递到 django 中的模态框进行编辑? - How can I pass information from an item to a Modal in django to edit it? 如何从Django错误消息中过滤信息? - How can I filter information from a Django error message? 如何在Django表单上显示数据库数据? - How can i display database data on a Django form? 如何在数据库中显示我的数据并将其导出为 pdf -Django - How can i display my data in database and export it to pdf -Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM