嗨,我是django的新手。
我想在我的网页上添加一个按钮来更改文字。
然而,它并没有发生 。 我无法理解要改变什么它不会给出任何错误my html page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>The current time</title>
</head>
<body>
<h1>My helpful timestamp site</h1>
<p>It is now {{ current_date }}.</p>
<form action="#" method="GET">
<p>It is {{ result }}</p>
<input type="text" name="q">
<input type="submit" name="summary">
</form>
<hr>
<p></p>
</body>
</html>
my views.py file
import textwrap
import json,httplib
import datetime
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from django.views.generic.base import View
from django.shortcuts import render
def HomePageView(request):
now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date': now}))
return HttpResponse(html)
def getJson(request):
if(request.GET.get('summary')):
html = t.render(Context({'result': 'this'}))
return HttpResponse(index.html)
我的urls.py文件
from django.conf.urls import patterns, url
from People.views import HomePageView
from People.views import getJson
urlpatterns = patterns(
'',
url(r'^', HomePageView),
url(r'^', getJson),
)
答案 0 :(得分:0)
您需要将表单发布到JSON视图。
形式;
<form action="{% url 'json_view' %}" method="POST">
<p>It is {{ result }}</p>
<input type="text" name="q">
<input type="submit" name="summary">
</form>
views.py文件
def HomePageView(request):
now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date': now}))
return HttpResponse(html)
def getJson(request):
t = get_template('index.html')
if 'summary' in request.POST:
html = t.render(Context({'result': 'this'}))
return HttpResponse(html)
return HttpResponse(index.html)
urls.py文件
urlpatterns = patterns(
'',
url(r'^', HomePageView, name='home'),
url(r'^json/$', getJson, name='json_view'),
)
答案 1 :(得分:0)
我通过在urls.py
中添加此代码解决了这个问题from django.conf.urls import patterns, url
from People.views import HomePageView
from People.views import getJson
from People import views
urlpatterns = patterns(
'',
url(r'^$', views.HomePageView ),
url(r'^getJson/$', views.getJson, name = 'getJson'),
)