我是Django的新手。我想制作一个简单的网络应用程序,其中包含一些股票的名称,然后你必须点击一个然后下一个窗口将是你想要的数据“获得百分比变化”,然后它将显示该股票的百分比变化。我知道代码的某些部分应该更好但我只是尝试Django的不同部分。我在第二部分遇到问题,选择股票后,我收到错误,我已尽力而为但无法将其删除。
这是我的urls.py文件:
# project/stocks.urls.py
from django.conf.urls import patterns, url
from stocks import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<stocks_id>\d+)/$', views.choice, name='choice'),
url(r'^(?P<stocks_id>\d+)/(?P<choice_id>\d+)/$', views.data, name='data')
)
这是我的观点文件:
# project/stocks.views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from stocks.models import Stocks, DataChoice
from NewStock_API import Get_Quote, Get_percent_change
from django.views import generic
def index(request):
latest_stocks = Stocks.objects.all()
context = {'latest_stocks': latest_stocks}
return render(request, 'stocks/index.html', context)
def choice(request, stocks_id):
total_choice = DataChoice.objects.all()
context = {'total_choice': total_choice}
return render(request, 'stocks/choice.html', context)
def data(request, stocks_id, choice_id):
selected_stock = Stocks.objects.get(id=stocks_id)
selected_symbol = selected_stock.symbol
stocks = Get_Quote(selected_symbol)
return render(request, 'stocks/data.html', {'stocks': stocks})
这是我的模特档案:
# project/stocks.models.py
from django.db import models
class Stocks(models.Model):
symbol = models.CharField(max_length=20)
def __unicode__(self):
return self.symbol
class DataChoice(models.Model):
choice_text = models.CharField(max_length=20)
def __unicode__(self):
return self.choice_text
我得到的错误是:
模板渲染期间出错
在模板/home/mukesh/markets/stocks/templates/stocks/choice.html中,第3行的错误 反向找不到参数'(1,)'和关键字参数'{}'的'data'。 1种模式尝试过:
[u'stocks/(?P<stocks_id>\\d+)/(?P<choice_id>\\d+)/$']
我的choice.html文件是:
<ul>
{% for choice in total_choice %}
<li><a href="{% url 'stocks:data' choice.id %}">{{ choice.choice_text }}</a></li>
{% endfor %}
</ul>
答案 0 :(得分:3)
您的数据网址模式有两个参数:stock id和choice id;你只提供一个。
您需要提供两者,如下所示:
{% url 'stocks:data' stocks_id=stocks.id choice_id=choice.id %}