objects.all()查询不起作用

时间:2015-06-03 21:11:58

标签: python django django-templates django-views

我正在尝试通过django为用户创建表单。用户(以下称为开发者)可以从主管列表中进行选择以使其自己注册。问题是,我没有从查询中获得所有主管的列表。当我使用objects.get()时,我收到一个错误,收到了2个对象。这意味着查询从数据库中获取行。

models.py

from django.db import models

class UserProfile(models.Model):
    name = models.CharField(max_length=50,verbose_name="Name")
    login = models.CharField(max_length=(25),verbose_name="Login")
    password = models.CharField(max_length=100, verbose_name="Password")
    phone = models.CharField(max_length=20, verbose_name="Phone number", null=True, default=None, blank=True)
    born_date = models.DateField(verbose_name="Born date" , null=True,default=None, blank=True)
    last_connection = models.DateTimeField(verbose_name="Date of last connection" , null=True, default=None, blank=True)
    email = models.EmailField(verbose_name="Email")
    years_seniority = models.IntegerField(verbose_name="Seniority", default=0)
    date_created = models.DateField(verbose_name="Date of Birthday", auto_now_add=True)

    def __str__(self):
        return self.name


class Supervisor(UserProfile):
    specialisation = models.CharField(max_length=50, verbose_name="Specialisation")

class Developer(UserProfile):
    supervisor = models.ForeignKey(Supervisor, verbose_name="Supervisor")

表单视图 create_developer.py -

from django.shortcuts import render
from django.http import HttpResponse
from TasksManager.models import Supervisor, Developer
# View for create_developer
def page(request):
    error = False
    # If form has posted
    if request.POST:
        if 'name' in request.POST:
            name = request.POST.get('name', '')
        else:
            error=True
        if 'login' in request.POST:
            login = request.POST.get('login', '')
        else:
            error=True
        if 'password' in request.POST:
            password = request.POST.get('password', '')
        else:
            error=True
        if 'supervisor' in request.POST:
            supervisor_id = request.POST.get('supervisor', '')
        else:
            error=True
        if not error:
            supervisor = Supervisor.objects.get(id = supervisor_id)
            new_dev = Developer(name=name, login=login, password=password,supervisor=supervisor)
            new_dev.save()
            return HttpResponse("Developer added")
        else:
            return HttpResponse("An error as occured")
    else:
        supervisors_list =  Supervisor.objects.all()
        return render(request, 'en/public/create_developer.html')

模板 create_developer.html

{% extends "base.html" %}
    {% block title_html %}
        Create Developer
    {% endblock %}
    {% block h1 %}
        Create Developer
    {% endblock %}
    {% block article_content %}
        <form method="post" action="{% url 'create_developer' %}" >


        <table>
            <tr>
                <td>Name</td>
                <td>
                    <input type="text" name="name" />
                </td>
            </tr>

            <tr>
                <td>Login</td>
                <td>
                    <input type="text" name="login" />
                </td>
            </tr>

            <tr>
                <td>Password</td>
                <td>
                <input type="text" name="password" />
                </td>
            </tr>

            <tr>
            <td>Supervisor</td>
            <td>
                <select name="supervisor">  
                    {% for supervisor in supervisors_list %}
                        <option value="{{ supervisor.id }}">{{ supervisor.name}}</option>
                    {% endfor %}
                </select>
            </td>
            </tr>

            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Valid" />
                </td>
            </tr>

        </table>
    </form>
{% endblock %}

主管选择列表应显示列表。但我在那里得到一个空列表。 {{supervisors_list | length}} 的输出为零。

如何调试? enter image description here

1 个答案:

答案 0 :(得分:2)

你必须将上下文传递给html:

from django.shortcuts import render
from django.http import HttpResponse
from TasksManager.models import Supervisor, Developer
# View for create_developer
def page(request):
    error = False
    # If form has posted
    if request.POST:
        if 'name' in request.POST:
            name = request.POST.get('name', '')
        else:
            error=True
        if 'login' in request.POST:
            login = request.POST.get('login', '')
        else:
            error=True
        if 'password' in request.POST:
            password = request.POST.get('password', '')
        else:
            error=True
        if 'supervisor' in request.POST:
            supervisor_id = request.POST.get('supervisor', '')
        else:
            error=True
        if not error:
            supervisor = Supervisor.objects.get(id = supervisor_id)
            new_dev = Developer(name=name, login=login, password=password,supervisor=supervisor)
            new_dev.save()
            return HttpResponse("Developer added")
        else:
            return HttpResponse("An error as occured")
    else:
        supervisors_list =  Supervisor.objects.all()
        return render(request, 'en/public/create_developer.html', {'supervisors_list' : supervisors_list})