我直接輸入地址 http://127.0.0.1:8000/boycott/ajaxTot/ 是可以打開的,但是通過httpResponseRedirect跳轉的地址卻是錯誤的?
django的路由系統(tǒng)我以為自己明白了,這個例子卻把自己弄糊涂了,為什么會出現127.0.0.1:8000/a/b/a/b.html的形式,我路由定義的明明是127.0.0.1:8000/a/b
下面是我的結構圖
主url.py
from django.contrib import admin
from django.urls import path,include,re_path
from boycott.views import index,login,list
urlpatterns = [
path('admin/', admin.site.urls),
path('boycott/', include(('boycott.urls','boycott'),namespace='boycott')),
]
app urls.py
from django.urls import path,re_path
from boycott import views
app_name='boycott'
urlpatterns = [
path('index/', views.index,name='index'), #第一啊個參數還不能寫 index.html
path('login/', views.login,name='login'),
path(r'list/<int:id>/', views.list,{'iid':222},name='list'),
path(r'register/', views.register),
re_path(r'^list/(?P<id>[0-9]+)/(?P<iid>[0-9]+)/$', views.list,name='list'),
path('index/delete/', views.delete,name='delete'),
path('ajaxTot/', views.ajaxTot,name='ajax'), #其實就是這里不明白,路由地址為什么會重復拼接?而我直接訪問 http://127.0.0.1:8000/boycott/ajaxTot/ 是正常的,而通過
re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
]
這是我index頁面的效果圖,我通過點擊刪除后,ajax會把點擊按鈕的id值發(fā)送給后臺,后臺也能接收到
views.py如下
from django.shortcuts import render
from django.http.response import HttpResponse
# from django.db import models
from django.http import HttpResponseRedirect
from django.shortcuts import render, render_to_response
# from boycott.models import *
from boycott import models
from django.http import HttpResponse
import re
# Create your views here.
def index(res):
ret = {'data': None, 'group': None}
usergroup = models.UserGroup.objects.all()
ret['group'] = usergroup
if res.method == 'POST':
hostname = res.POST.get('hostname', None)
ip = res.POST.get('m_ip', '8.8.8.88')
user_group = res.POST.get('usergroup', None)
models.Asset.objects.create(hostname=hostname, ip=ip, user_group_id=user_group)
data = models.Asset.objects.filter(bDel=1)
ret['data'] = data
return render(res, 'boycott/index.html', ret)
def login(res):
print(res)
ret = {'status': '初始化變量'}
if res.method == 'POST':
username = res.POST.get('username', None)
password = res.POST.get('password', None)
print(username, password) # 數據是正常傳輸的
is_auth = all([username, password])
print(is_auth) # 只有都輸入的才進行匹配處理
if is_auth:
count = models.Userinfo.objects.filter(username=username, password=password).count()
# print(models.Userinfo.objects.all().values('password')) #輸出password列
# from django.db import models
# count = models.Userinfo.objects.filter(username=username,password=password).count()
# 我這里出問題,就是因為import錯了錯誤的對象,導致django.db.models沒有Userinfo屬性
# print(count)
if count == 1:
# return HttpResponseRedirect("https://www.baidu.com/")
return HttpResponseRedirect("/boycott/index")
# ret['status'] = 'Success'
else:
ret['status'] = '用戶名或密碼錯誤'
else:
ret['status'] = '用戶名或密碼不能為空'
return render(res, 'boycott/login.html', {'status': ret['status']})
# https://docs.djangoproject.com/en/2.0/ref/request-response/
def list(res, id, iid):
print(id, iid)
return HttpResponse('list/' + str(id) + '/' + str(iid))
def ajaxTot(res):
ret = {'data':None}
print(res.GET)
# print(res.POST)
var_p = res.GET.get('p')
if re.match('del_id_',str(var_p),flags=0): #這里需要把參數轉字符串,否則會報錯
# return render(res, 'boycott/ajaxTot.html', ret)
return HttpResponseRedirect('boycott/ajaxTot.html')
else:
return HttpResponse('<h1>ajax is NOT OK!</h1>')
但是我控制臺看到的 ajax的發(fā)送地址確實 a/b/a/b 但我url沒有這么指定
index.html如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>機器列表</title>
{# <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>#}
<script type="text/javascript" src="../../static/main.js"></script>
</head>
<body>
<div>
<form action="{% url 'boycott:index' %}" method="post">
{% csrf_token %}
<label for="hostname">主機名</label><input type="text" id="hostname" name="hostname" placeholder="請輸入主機名">
<label for="m_ip">IP地址</label><input type="text" id="m_ip" name="m_ip" placeholder="請輸入IP地址">
<select name="usergroup" id="usergroup">
{% for item in group %}
<option value={{ item.id }}>{{ item.GroupName }}</option>
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</div>
<div class="machine_list">
<table border="1">
<tr>
<td>主機名</td>
<td>IP</td>
<td>用戶組</td>
<td>刪除</td>
</tr>
{% for item in data %}
<tr>
<td>{{ item.hostname }}</td>
<td>{{ item.ip }}</td>
<td>{{ item.user_group.GroupName }}</td>
<td><input type="button" onclick=loadXMLDoc(this) value="刪除" id="del_id_{{ item.id }}" class="btn_class"></td>
{% comment %}
兩點不是很明白,點擊按鈕的時候,是否會給服務器發(fā)數據?
{% endcomment %}
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
發(fā)送ajax的js代碼如下
function loadXMLDoc(e){
console.log("標志進入xml函數"+e.id);
var xml;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xml=new XMLHttpRequest();
}
else{// code for IE6, IE5
xml=new ActiveXObject("Microsoft.XMLHTTP");
}
xml.onreadystatechange=function(){
if (xml.readyState==4 && xml.status==200){
console.log("點擊的按鈕id為"+e.id);
console.log("服務器返回的內容為:"+xml.responseText)
}
}
xml.open("GET","../ajaxTot/?p="+e.id,true); //這里不要多此一舉,寫成"../templates/ajaxTot/"
xml.send();
}
views.py中的def ajaxTot(res)我若是把紅框兩個位置注釋互換位置,則控制臺是可以正常顯示出來我的頁面內容,而不是頁面跳轉,這個問題困擾了好久,請高人解答,謝謝
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數據專業(yè)的國家
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產業(yè)為響應國家深化產教融合/校企合作的政策,積極推進“中國制造2025”,實現中華民族偉大復興的升級產業(yè)鏈。利用北京大學優(yōu)質教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯網管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網頁制作和網頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。