我正在尝试我的第一个Django应用程序。以前我使用Flask构建了以下应用程序,但它确实有效。
我希望我的应用搜索github并返回前5个搜索项。
我的应用程序名为assign1。 assign1 / views.py如下
from django.shortcuts import render
from django.template import loader
from .application import getresult
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def search(request):
search_term = request.GET['search_term']
t = loader.get_template('result.html')
c = Context({'items':getresult(search_term),'search_term':search_term,})
return HttpResponse(t.render(c))
assign1 / urls.py如下:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^result/$', views.search, name='search'),
]
我的assign1 / templates / index.html
<!DOCTYPE html>
{% block content %}
<div class = "container">
<h1>Search Github</h1>
<form action='/assign1/result'>
<input type='text' name='search_term'><br>
<br><br>
<input type='submit' value='Submit'>
</form>
{% endblock %}
我的result.html如下:
<!DOCTYPE html>
<head><title>Github Navigator</title></head>
<body>
{% block content %}
<h1>{{search_term}}</h1>
{% for rep in items %}
<h2>{{loop.index}} {{rep["respository_name"]}}</h2>
<h3> Created {{rep["created_at"]}}</h3>
<a href="{{rep["owner_url"]}}"><img src="{{rep["avatar_url"]}}" alt="avatar" height="42" width="42"/></a>
{{rep["owner_login"]}}
<h3>LastCommit</h3>
{{rep["sha"]}} {{rep["commit_message"]}} {{rep["commit_author_name"]}}
<hr/>
{% endfor %}
{% endblock %}
</body>
索引打开正常。当我点击按钮时,网站网址为http://127.0.0.1:8000/result?search_term=arrow
我收到一条错误,说找不到页面
Using the URLconf defined in assignment.urls, Django tried these URL patterns, in this order:
^assign1/
^admin/
The current URL, result, didn't match any of these
我的主要urls.py如下:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Examples:
# url(r'^$', 'myproject.views.home', name = 'home'),
# url(r'^blog/', include('blog.urls')),
url(r'^assign1/', include('assign1.urls')),
url(r'^admin/', admin.site.urls),
]
我做错了什么?