Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

After submitting form Redirect to home page by using Django, python with vue axiox

When user filled form then after submitting form form needs to redirect to another page. I have tried but I am new to vue so, I am not able to understand how to achieve this please help me to achieve this thank you.

html

<div id="app">
    <form>
        <input type="text" v-model="username">
        <input type="password" v-model="password">
        <input v-on:click.prevent="submitForm" type="submit" value="submit">
    </form>

</div>

vue.js

<script>

const vms = new Vue({
    delimiters: ['[[', ']]'],
  el: '#app',
        data: {
          username: null,
          password: null,

          success_msg: "",
          err_msg: "",

        },
        /* submiting post Ad form */
        methods: {
          submitForm: function(){
            axios({
                method : "POST",
                url:"{% url 'submitform' %}", //django path name
                headers: {'X-CSRFTOKEN': '{{ csrf_token }}',},
                data : {"username":this.username, "password":this.password},//data
              }).then((response) => {
              console.log(
             this.posts.push(response.data);
          });
          },

        },

    });
    Vue.config.productionTip = false


</script>

This is my view code to save data into databse. views.py



from django.shortcuts import render
from django.http import JsonResponse
from .models import vue_testing
import json
def submit_form(request):
    if request.method == "POST":
        data = json.loads(request.body)
        username = data['username']
        print("username", str(username))
        # password = data['password']
        # print("password", str(password))
        saveform = vue_testing(username=username,
                               )
        saveform.save()
        return redirect("/")
        # if username and password:
        #     response = f"Welcome {username}"
        #     return JsonResponse({"msg":response}, status=201)
        # else:
        #     response = "username or password is empty"
        #     return JsonResponse({"err":response}, status=400)
    return render(request, 'testing_vue.html')


question from:https://stackoverflow.com/questions/65886145/after-submitting-form-redirect-to-home-page-by-using-django-python-with-vue-axi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can you try these modifications :

from django.shortcuts import render
from django.http import JsonResponse
from .models import vue_testing
import json


def submit_form(request):
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
      
        if username and password:
            saveform = vue_testing.objects.create(username=username, password=password)
            response = f"Welcome {username}"
            return JsonResponse({"msg": response}, status=201)
        else:
            response = "username or password is empty"
            return JsonResponse({"err": response}, status=400)

    return render(request, 'testing_vue.html')

vue.js

<script>
const vms = new Vue({
    delimiters: ['[[', ']]'],
  el: '#app',
        data: {
          username: null,
          password: null,

          success_msg: "",
          err_msg: "",

        },
        /* submiting post Ad form */
        methods: {
          submitForm: function(){
            axios({
                method : "POST",
                url:"{% url 'submitform' %}", //django path name
                headers: {'X-CSRFTOKEN': '{{ csrf_token }}',},
                data : {"username":this.username, "password":this.password},//data
              }).then((response) => {
                console.log(response)
                window.location.replace('{% url "home" %}')  // Replace home by the name of your home view
              }).catch((error) => {
                console.log(error)
              });
          },
        },
    });
    Vue.config.productionTip = false
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...