Django namespace, from form, get method of ajax and post method

django namespace

  • set namespace for urls
from django.urls import path
from . import views

app_name = 'news'
urlpatterns = [
    path('', views.news, name='news'),
    path('news_detail/<news_id>/', views.news_detail, name='news_detail'),
    path('news_list/', views.news_list, name='news_list'),
]

  • Use of namespaces
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>新闻首页</p>
	<!--  <p><a href="news_detail/">新闻详情页</a></p>  -->
    <p><a href="{% url 'news:news_detail' 1 %}">新闻详情页</a></p>
</body>
</html>

django的ajax

  • from form
    -get method

        <from action="/text/" method="get">
            user: <input type="text">
            pwd: <input type="text">
            <input type="submit">
        </from>
    

    verify:

    def text_ajax(request):
        if request.method == 'GET':
            name = request.GET.get('name')
            pw = request.GET.get('pw')
            print(request.method)
            print(name)
            print(pw)
    
    • post needs to add csrftoken, everything else is the same
  • The use of ajax
    in django has troubled me for a few days. The get method can be used directly, and the post also needs to obtain the csrftoken. Since I didn't pay much attention to the information displayed in the django console, I have been looking at the information of the browser. The error is 500, which is an internal server error. I thought it was unable to find the route. As a result, I checked the Internet for a few days, but there was no result. Today, I saw the console information by chance, and I realized that the scrftoken was not carried.

    • in urls

      from django.urls import path
      from . import views
      
      app_name = 'text_ajax'
      urlpatterns = [
          path('text/', views.text_ajax, name='text_ajax'),
      ]
      
    • html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
          <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
      </head>
      <body>
      <h3>功能1:发送ajax请求</h3>
      <p class="content"></p>这里的内容是空的
      <button class="btn">ajax</button>
      <p>
          <button id="btn">post</button>
      </p>
      <script>
          $('.btn').click(function () {
               
               
              $.ajax({
               
               
                  url: '/text/text/',
                  type: 'get',
                  data: {
               
               name: 'zs', pw: "123"},
                  success: function (data) {
               
               
                      console.log(data)
                      // $('.content').html(data)
                  }
              })
          })
      
          function getCookie(name) {
               
               
              var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
              return r ? r[1] : undefined;
          }
      
          $('#btn').click(function () {
               
               
              $.ajax({
               
               
                  url: '/text/text/',
                  type: 'post',
                  data: {
               
               name: 'zs', pw: "123"},
                  success: function (data) {
               
               
                      console.log(data)
                      $('.content').html(data)
                  },
                  headers: {
               
               
                      "X-CSRFToken": getCookie("csrftoken")
                  },
              })
          })
      </script>
      </body>
      </html>
      
    • views

      from django.shortcuts import render
      from django.http import HttpResponse, JsonResponse
      import json
      
      
      # Create your views here.
      def index(request):
          return render(request, "text_ajax.html")
      
      
      def text_ajax(request):
          if request.method == 'GET':
              name = request.GET.get('name')
              pw = request.GET.get('pw')
              print(request.method)
              print(name)
              print(pw)
          elif request.method == 'POST':
              name = request.POST.get('name')
              pw = request.POST.get('pw')
              print(request.method)
              print(name)
              print(pw)
              return HttpResponse("hello world!")
      
          return JsonResponse({
              
              "data": "hello world!"})
      
          # data = json.loads(request.body)
          # user = data.get('name')
          # print(user)
          # return HttpResponse(user)
      
      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324153911&siteId=291194637