Django:我无法在网站

时间:2017-07-22 17:59:19

标签: python django

我正在设置用户注册/登录/搜索程序,当我尝试通过电子邮件查找用户并在网站上显示它们时,它们就像在数据库中找不到它一样,但它& #39;那里。

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

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

<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>

我无法弄清楚我做错了什么。 有人能指出我正确的方向吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

这部分代码毫无意义:

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

也许你的意思是:

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