【发布时间】:2019-05-27 06:38:26
【问题描述】:
这里是 Django 新手。对于我的第一个项目,我正在学习我在网上找到的一门课程,这是一个寻找房产、房地产经纪人和其他东西的网站。
在登录页面上,我有一个搜索表单,可用于根据位置、价格等查找属性。但是,在提交表单时,它给了我以下错误。
TypeError at /listings/search
listing() missing 1 required positional argument: 'listing_id'
Request Method: GET
Request URL: http://localhost:8000/listings/search?keywords=&city=
Django Version: 2.1.4
Exception Type: TypeError
Exception Value:
listing() missing 1 required positional argument: 'listing_id'
Exception Location: /home/nived/Documents/BTRE_PROJECTF/venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 124
Python Executable: /home/nived/Documents/BTRE_PROJECTF/venv/bin/python
Python Version: 3.6.7
Python Path:
['/home/nived/Documents/BTRE_PROJECTF',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/nived/Documents/BTRE_PROJECTF/venv/lib/python3.6/site-packages']
我不确定这里指的是哪个listing_id。
到目前为止,我还没有在 search.html 文件中添加任何标记。它只是一个简单的
<h1>Search</h1>
在开始添加更多内容之前,我想确保页面链接正确。
索引页面html
<section id="showcase">
<div class="container text-center">
<div class="home-search p-5">
<div class="overlay p-5">
<h1 class="display-4 mb-4">
Property Searching Just Got So Easy
</h1>
<p class="lead">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Recusandae quas, asperiores eveniet vel nostrum magnam
voluptatum tempore! Consectetur, id commodi!</p>
<div class="search">
<form action="{% url 'search' %}">
<!-- Form Row 1 -->
<div class="form-row">
<div class="col-md-4 mb-3">
<label class="sr-only">Keywords</label>
<input type="text" name="keywords" class="form-control" placeholder="Keyword (Pool, Garage, etc)">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">City</label>
<input type="text" name="city" class="form-control" placeholder="City">
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">State</label>
<select name="state" class="form-control">
<option selected="true" disabled="disabled">State (All)</option>
{%for key,value in state_choices.items%}
<option value="{{key}}">{{value}}</option>
{%endfor%}
</select>
</div>
</div>
<!-- Form Row 2 -->
<div class="form-row">
<div class="col-md-6 mb-3">
<label class="sr-only">Bedrooms</label>
<select name="bedrooms" class="form-control">
<option selected="true" disabled="disabled">Bedrooms (All)</option>
{%for key,value in bedroom_choices.items%}
<option value="{{key}}">{{value}}</option>
{%endfor%}
</select>
</div>
<div class="col-md-6 mb-3">
<select name="price" class="form-control" id="type">
<option selected="true" disabled="disabled">Max Price (Any)</option>
{%for key,value in price_choices.items%}
<option value="{{key}}">{{value}}</option>
{%endfor%}
</select>
</div>
</div>
<button class="btn btn-secondary btn-block mt-4" type="submit">Submit form</button>
</form>
</div>
</div>
</div>
</div>
</section>
列表应用程序中的 urls.py
from django.urls import path
from . import views
urlpatterns=[
path('', views.index,name='listings'),
path('<int:listing_id>',views.listing,name='listing'),
path('search',views.listing,name='search'),
]
列表应用中的views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from .models import Listing
def index(request):
listings = Listing.objects.order_by('-list_date') .filter(is_published=True)
paginator = Paginator(listings,6)
page=request.GET.get('page')
paged_listings=paginator.get_page(page)
context={
'listings':paged_listings
}
return render(request, 'listings/listings.html',context)
def listing(request,listing_id):
listing=get_object_or_404(Listing,pk=listing_id)
context = {
'listing' : listing
}
return render(request,'listings/listing.html',context)
def search(request):
return render(request, 'listings/search.html')
这里似乎有什么问题?欢迎任何建议。
【问题讨论】:
-
1) 你确定你的 url 命名空间正确吗? urls.py 应该有一个 app_name='listings'... 2) 问题具体出现在 Listings/search/.请确保您的 search.html 确实如您所述为空。 ..3)如果您在浏览器中的模板中遇到错误,请分析完整的回溯并给出发生错误的确切行
-
我的网址间距正确。列表和列表方法都可以正常工作。 Search.html 只是将搜索写在 h1 标签下。
-
该问题具体出现在 search.html 中。如果你有代码托管在网上(比如 github),也许你可以分享链接
-
@coderDude :感谢所有帮助。刚想通,真是愚蠢的错误!我错误地配置了我的网址。它把我带到了views.listing,但我希望它把我带到views.search 方法。复制粘贴同一行后忘记更改查看方法。目前一切都很好。干杯!