2015-06-03 173 views
1

我想通過django爲用戶創建一個窗體。用戶(以後稱爲開發者)可以從主管列表中選擇自己註冊。問題是,我沒有從查詢中獲得所有主管的列表。當我使用objects.get()時,我收到一個錯誤,收到2個對象。這意味着查詢正在從數據庫中獲取行。objects.all()查詢不起作用

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

它看起來就像你不及格supervisors_list到渲染功能方面。 –

+2

與您自己處理密碼和用戶身份驗證信息相反,請查看django內置的用戶模型。如果它不能完全滿足您的需求,那麼定製它很簡單(請參閱最後的示例)https://docs.djangoproject.com/en/1.8/topics/auth/customizing/ – Ben

+0

謝謝,我將使用:) – goelakash

回答

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}) 
+0

謝謝,這工作! – goelakash