skip to Main Content

Thats my first project in Django. I want to make table and add items with modal form. I use Mysql database. items which addes manually from phpmyadmin already exist on table but when i try add from modal form it cant added.
views.py

from django.shortcuts import render
from django.http import HttpResponse


from .models import Client

def viewpost(request):

    post_list = Client.objects.all()
    context = {
        'posts': post_list
    }
    return render(request, 'mysite/viewtable.html', context)

def add_client(request):
    if request.method == 'POST':
        post = Client()
        post.name = request.POST.get('name')
        post.surname = request.POST.get('surname')
        post.address = request.POST.get('address')
        post.gender = request.POST.get('gender')
        post.age = request.POST.get('age')
        post.save()

        return render(request, 'mysite/viewtable.html')

    else:
        return render(request, 'mysite/viewtable.html')

url.py:

from django.urls import path
from . import views
urlpatterns = {
    path('viewtable/', views.viewpost, name='viewpost'),
    path('viewtable/#add_data_Modal', views.add_client, name='add_client'),
}

2

Answers


  1. Chosen as BEST ANSWER

    Before the table and modal form didnt work together. If table worked then modal form didnt add to database or conversely. Now the're work together: Modal form add to database then table show data on page. But when i refresh page data add to databse twice. views.py:

    def viewpost(request):
        post_list = Persona.objects.all()
        context = {
            'posts': post_list
        }
        if request.method == 'POST':
            if request.POST.get('name') and request.POST.get('surname') and request.POST.get('address'):
                post = Client()
                post.name = request.POST.get('name')
                post.surname = request.POST.get('surname')
                post.address = request.POST.get('address')
                post.age = request.POST.get('age')
                post.save()
    
    
    
                return render(request, 'mysite/viewtable.html', context)
    
        else:
            return render(request, 'mysite/viewtable.html')
    

  2. Thats SOLVED. Here’s view

    def viewpost(request):
            post_list = Persona.objects.all()
    
    
            if request.method == 'POST':
                if request.POST.get('name') and request.POST.get('surname') and 
                request.POST.get('address'):
                    post = Persona()
                    post.name = request.POST.get('name')
                    post.surname = request.POST.get('surname')
                    post.address = request.POST.get('address')
                    post.age = request.POST.get('age')
                    post.save()
    
    
                    return HttpResponseRedirect('/viewtable')
    
            else:
    
                return render(request, 'mysite/viewtabel.html', {'posts': post_list}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search