2016-08-13 99 views
-2

我想寫一個簡單的銀行應用程序使用Django我被困在一個奇怪的錯誤,缺少位置參數。任何幫助將非常感激。其他模板和視圖可以正常工作。當我進入登錄頁面時會發生問題。Django TypeError缺少1個必需的位置參數:'person_id'

完整的錯誤是:

TypeError at /banking/login/ 

login() missing 1 required positional argument: 'person_id' 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/banking/login/ 
Django Version:  1.9 
Exception Type:  TypeError 
Exception Value:  

login() missing 1 required positional argument: 'person_id' 

Exception Location:  /home/soupdragon/DJapps/env/lib/python3.4/site- 
packages/django/core/handlers/base.py in get_response, line 147 
Python Executable: /home/soupdragon/DJapps/env/bin/python 
Python Version:  3.4.3 
Python Path:  

'/home/soupdragon/DJapps/banking/mybank', 
'/home/soupdragon/DJapps/env/lib/python3.4', 
'/home/soupdragon/DJapps/env/lib/python3.4/plat-i386-linux-gnu', 
'/home/soupdragon/DJapps/env/lib/python3.4/lib-dynload', 
'/usr/lib/python3.4', 
'/usr/lib/python3.4/plat-i386-linux-gnu', 
'/home/soupdragon/DJapps/env/lib/python3.4/site-packages'] 

Server time: Fri, 12 Aug 2016 17:39:38 +0000 

堆棧跟蹤爲:

Internal Server Error: /banking/login/ 
Traceback (most recent call last): 
File "/home/soupdragon/DJapps/env/lib/python3.4/site-packages/django 
/core/handlers/base.py", line 149, in get_response 
response = self.process_exception_by_middleware(e, request) 
File "/home/soupdragon/DJapps/env/lib/python3.4/site-packages/django 
/core/handlers/base.py", line 147, in get_response 
response = wrapped_callback(request, *callback_args, **callback_kwargs) 
TypeError: login() missing 1 required positional argument: 'person_id' 
[12/Aug/2016 17:39:38] "GET /banking/login/ HTTP/1.1" 500 6010 

的URL文件:

from django.conf.urls import url 

from . import views 

app_name = 'banking' 
urlpatterns = [ 
# ex: /banking/deposit 
url(r'^deposit/$', views.deposit, name='deposit'), 
# ex: /banking/ 
url(r'^$', views.hello, name='hello'), 
# ex: /banking/login 
url(r'^login/$', views.login, name='login'), 
# ex: /banking/transfer 
url(r'^transfer/$', views.transfer, name='transfer'), 
# ex: /banking/withdraw 
url(r'^withdraw/$', views.withdraw, name='withdraw'), 
# ex: /banking/welcome 
url(r'^welcome/$', views.welcome, name='welcome'), 
] 

的模型文件是:

from django.db import models 

# Create your models here. 
class Person(models.Model): 
    first_name = models.CharField(max_length=4,default="Bob") 
    last_name = models.CharField(max_length=6,default="Smith") 
    login_name = models.CharField(max_length=3,default="me") 
    password = models.CharField(max_length=7, default="password") 
    person_text = models.CharField(max_length=10, default="Bob Smith") 

    def get_first_name(self): 
     return self.first_name 

    def get_last_name(self): 
     return self.last_name 

    def get_login_name(self): 
     return self.login_name 

    def get_password(self): 
     return self.password 

    def __str__(self): 
     return self.person_text 

class Account1(models.Model): 
    account_number = models.IntegerField(default=12345678) 
    bank_balance = models.DecimalField(max_digits=4, 
    decimal_places=2,default=12.99) 
    interest_rate = models.IntegerField(default=2) 
    account_name = models.CharField(max_length=16, default="Current 
        Account") 
    person = models.ForeignKey(Person,      on_delete=models.CASCADE,default="") 

    def get_account_number(self): 
     return self.account_number 

    def get_bank_balance(self): 
     return self.bank_balance 

    def deposit(self, amount): 
     self.bank_balance = self.bank_balance + amount 
     return 

    def get_interest_rate(self): 
     return self.interest_rate 

    def withdraw(self, amount): 
     self.bank_balance = self.bank_balance - amount 
     return 

    #def __str__(self): 
    # return self.account_name 

意見文件是:

from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.template import loader 

from .models import Person, Account1 

# Create your views here. 
def deposit(request): 
    person = Person 
    account1 = Account1 
    context = {'account1':account1,'person':person} 
    template = loader.get_template('banking/deposit.html') 
    return HttpResponse(template.render(context,request)) 

def hello(request): 
    return HttpResponse("Hello.") 

def login(request, person_id): 
    person = get_object_or_404(Person, pk=person_id) 
    try: 
     selected_login = person.choice_set.get(pk=request.POST['login']) 
     selected_password = person.choice_set.get(pk=request.POST['password']) 
    except (KeyError, Choice.DoesNotExist): 
     # Redisplay the question voting form. 
     return render(request, 'banking/login.html', { 
      'person': person, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     # Always return an HttpResponseRedirect after successfully dealing 
     # with POST data. This prevents data from being posted twice if a 
     # user hits the Back button. 
     return HttpResponseRedirect(reverse('banking:welcome', 
      args=(question.id,))) 


def transfer(request): 
    person = Person 
    account1 = Account1 
    context = {'account1':account1,'person':person} 
    template = loader.get_template('banking/transfer.html') 
    return HttpResponse(template.render(context,request)) 

def welcome(request): 
    person = Person 
    account1 = Account1 
    context = {'account1':account1,'person':person} 
    template = loader.get_template('banking/welcome.html') 
    return HttpResponse(template.render(context,request)) 

def withdraw(request): 
    person = Person 
    account1 = Account1 
    context = {'account1':account1,'person':person} 
    template = loader.get_template('banking/withdraw.html') 
    return HttpResponse(template.render(context,request)) 

登錄模板:

{% load staticfiles %} 
<!DOCTYPE html> 
<html> 
<head> 
<title>Deposit Money</title> 
<h1 id="bluetext">Please Select Account to Deposit into</h1> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="{% static 'css/deposit.css' %}"> 
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap 
/3.3.5/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery 
/1.11/jquery.min.js"></script> 
<style type="text/css"></style> 
</head> 
<body> 

<div class="container-fluid" style="background-image: 
url('login.jpg'); width: 100%; height: 300px; background-size:cover;"> 
</div> 

<form action="{% url 'banking:login' person.id %}" method="post"> 
<fieldset> 
{% csrf_token %} 
<legend>Your details:</legend> 
<hr></hr> 
Username:<input type="text" id="username" name="username" size="33 
</input> 
<br></br> 
Password:<input type="text" id="password" name="password" size="33"> 
</input> 
<input type="submit" name="submit" value="Submit"></input> 
</fieldset> 
</form> 

</body> 
</html> 
+2

請使用內置的認證系統。使用未經安全審查的身份驗證系統不僅會使您的網站處於危險之中,還會泄露隱私信息或將用戶的帳戶置於其他網站上。將密碼保存爲純文本不安全。 – knbk

+0

出於某種原因,您的登錄視圖期待person_id,但您沒有在URL中捕獲該值。請注意,這些都沒有任何意義;你沒有選擇模型,但不清楚爲什麼你要檢查用戶名和密碼,而不是人本身。無論如何,正如knbk所說,你**不能**自行認證。 –

回答

1

的問題是在這裏:

def login(request, person_id): 
    person = get_object_or_404(Person, pk=person_id) 

你的觀點預計person_id從URL中通過,但你網址格式不包括person_id

請不要重新發明輪子 - 使用內置的認證系統。如果必須,您應該嘗試從request.POST中檢索usernamepassword,並使用用戶名來查找正確的用戶(人)。無論如何,使用person_id可能不是最好的解決方案。

+0

非常感謝你的答覆。我不知道有一個內置的認證系統。我會讀這些文件。 – greatchicken

+0

我試着添加selected_login = request.POST.get('login')和selected_username = request.POST.get('username'),並刪除了person_id並更新了url並且工作正常。有時間閱讀Django文檔! – greatchicken

+0

如果答案解決了您的問題,您可能想要接受答案。 – masnun

相關問題