在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ django路由系統(tǒng)為什么越拼接越長?

django路由系統(tǒng)為什么越拼接越長?

我直接輸入地址 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)我若是把紅框兩個位置注釋互換位置,則控制臺是可以正常顯示出來我的頁面內容,而不是頁面跳轉,這個問題困擾了好久,請高人解答,謝謝

回答
編輯回答
她愚我

views的ajaxTot方法里

return HttpResponseRedirect('boycott/ajaxTot.html')

改成

return HttpResponseRedirect('/boycott/ajaxTot.html')
2017年8月16日 04:52
編輯回答
胭脂淚

重定向的話參數應該是 視圖函數的名字。不是模板頁面的路徑,我記得是這樣,你再看看。

我一般是這么寫重定向的,你看一下。比如登出后重定向到登錄界面。

@login_required
def log_out(request):
    logout(request)
    return redirect('log_in')
2017年9月7日 19:17