Home > database >  Why i can not able to update my data in django
Why i can not able to update my data in django

Time:06-16

I can not able to edit data in my django project

settings.py

Django settings for superAdmin project.

Generated by 'django-admin startproject' using Django 4.0.4.


import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR =os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')



SECRET_KEY = 'django-insecure-pqeid(n@vcuz-nj_ qphdlmcabz58w7cb#h3)hidden'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sa',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'superAdmin.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'superAdmin.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.mysql',
        'NAME': 'super',
        'USER' : 'root',
        'PASSWORD': '',
        'HOST':'localhost',
        'PORT':'3306'
    },
    'sales': {
        'ENGINE':'django.db.backends.mysql',
        'NAME': 'salesteam',
        'USER' : 'root',
        'PASSWORD': '',
        'HOST':'localhost',
        'PORT':'3306'
    },
    'client': {
        'ENGINE':'django.db.backends.mysql',
        'NAME': 'django',
        'USER' : 'root',
        'PASSWORD': '',
        'HOST':'localhost',
        'PORT':'3306'
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'
STATICFILES_DIRS =[STATIC_DIR,]
MEDIA_ROOT ='/media/'

MEDIA_ROOT =os.path.join(BASE_DIR,'media')

STATICFILES_DIRS =(
        os.path.join(BASE_DIR,'static'),
)

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# EMAIL CONFIGURATION

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'ixwxkfeunbdrabfj' #past the key or password app here
EMAIL_PORT = 587
EMAIL_USE_TLS = True

models.py

from django.db import models

class sa_storesheet(models.Model):
    id=models.IntegerField(max_length=100, primary_key=True)
    store_name=models.CharField(max_length=100)
    date=models.DateField(max_length=100)
  
    product_name=models.CharField(max_length=100)
    qty=models.IntegerField(max_length=100)
    product_cost=models.FloatField(max_length=100)
    sale_price_shipping=models.FloatField(max_length=100)
    cogs = models.FloatField(max_length=100)
    gross_profit = models.FloatField(max_length=100)
    amazon_fee= models.FloatField(max_length=100)
    supplier_tax= models.FloatField(max_length=100)
    supplier_shipping_cost= models.FloatField(max_length=100)
    amazon_administrative = models.FloatField(max_length=100)
    net_profit=models.FloatField(max_length=100)
    befrugal = models.FloatField(max_length=100)
    cash_back_profit = models.FloatField(max_length=100)
    total_profit = models.FloatField(max_length=100)
    shipping_status = models.CharField(max_length=100)
    ordered_status = models.CharField(max_length=100)
    after_delivery_status = models.CharField(max_length=100)
    supplier_name = models.CharField(max_length=100)
    customer_name = models.CharField(max_length=100)
    customer_mobile=models.CharField(max_length=100)
    customer_address=models.CharField(max_length=100)
    purchase_order_id = models.CharField(max_length=100)
    amazon_order_id = models.CharField(max_length=100)
    amazon_tax = models.FloatField(max_length=100)
    tracking_number = models.CharField(max_length=100)
    o_a_email_address = models.CharField(max_length=100)
   
    notes = models.CharField(max_length=200)
    editor = models.CharField(max_length=100)
    class Meta:
        managed = False
        db_table="all_store"

sa_storesheet.objects = sa_storesheet.objects.using('sales')

view.py

import datetime
from django.shortcuts import render
from django.shortcuts import redirect
from django.contrib.auth import authenticate, login ,logout
from django.contrib.auth.models import User
from sa.models import sa_storesheet
from django.contrib import messages
from django.core.mail import send_mail
from django.conf import settings
from django.template.loader import render_to_string
from sa.models import client_info,sales_store_name,client_store_name,sales_info
import uuid,re

def sa_search(request):
    if request.user.is_authenticated:
        username = request.user.username
        if request.method=="POST":
            result1=client_store_name.objects.using('client').all()
            o_id = request.POST.get('search','')
            result2 =sa_storesheet.objects.using('sales').all().filter(amazon_order_id=o_id).values()

            return render(request,'search.html',{'sa_storesheet':result2 })
    
        if request.method=="POST":
                product_name=request.POST.get('pro_na')
                product_cost=float(request.POST.get('pro_cost'))
                quantity= eval(request.POST.get('qty'))
                sps = eval(request.POST.get('sps'))
                cogs = float(request.POST.get('cogs'))
                gp = float(request.POST.get('gp'))
                amazon_fee = float(request.POST.get('af'))
                supp_tax = eval(request.POST.get('st'))
                ssc = eval(request.POST.get('ssc'))
                aaf = eval(request.POST.get('aaf'))
                net_profit =float(request.POST.get('nf'))
                total_cogs = cogs   supp_tax   ssc
                order_s = request.POST.get('order_sta')
                ship = request.POST.get('shipped_sta')
                supplier_n = request.POST.get('supplier')
                customer_n = request.POST.get('customer')
                order_id = request.POST.get('orderID')
                A_order_id = request.POST.get('aoi')
                amazon_tax = eval(request.POST.get('ama_tax'))
                customer_mobile =request.POST.get('mobile')
                customer_add = request.POST.get('address')
                notes = request.POST.get('note')
                date = "2020-06-11"
                bef =0
                store_na ='bclawx'
                cash = 0
                total_p=15
                after = "null"
                saverecord3 = sa_storesheet.objects.using('sales').get(amazon_order_id=request.POST.get('aoi'))
                saverecord3.store_name = store_na
                saverecord3.date = date
                saverecord3.product_name = product_name
                saverecord3.qty =quantity
                saverecord3.product_cost = product_cost
                saverecord3.sale_price_shipping =sps
                saverecord3.cogs = cogs
                saverecord3.gross_profit = gp
                saverecord3.amazon_fee =amazon_fee
                saverecord3.supplier_tax =supp_tax
                saverecord3.supplier_shipping_cost =ssc
                saverecord3.amazon_administrative =aaf
                saverecord3.net_profit =net_profit
                saverecord3.befrugal = bef
                saverecord3.cash_back_profit =cash
                saverecord3.total_profit = total_p
                saverecord3.shipping_status =ship
                saverecord3.ordered_status =order_s
                saverecord3.after_delivery_status = 'null'
                saverecord3.supplier_name =supplier_n
                saverecord3.customer_name =customer_n
                saverecord3.customer_mobile = customer_mobile
                saverecord3.customer_address =customer_add
                saverecord3.purchase_order_id =order_id
                saverecord3.amazon_order_id =A_order_id
                saverecord3.amazon_tax =amazon_tax
                saverecord3.tracking_number ="null"
                saverecord3.o_a_email_address = "null"
                
                saverecord3.notes = notes
                saverecord3.editor = "ADMIN"
                saverecord3.save()
                return render(request,'search.html')
        else:
            return render(request,'search.html')
    else:
        return redirect('/superlogin')

in def_sa_search, I search product id in input box and when i hit enter ,it loaded data and put on html page, but when i change value ,and hit update data button. it doesnot change

Here is my search panel

here product information shown in input box

when i click update data input type button..it doesnot change

Here is my video link:Click

<!DOCTYPE html>
{% load static %}
<html  lang="en" data-textdirection="ltr">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="description" content="Chameleon Admin is a modern Bootstrap 4 webapp &amp; admin dashboard html template with a large number of components, elegant design, clean and organized code.">
    <meta name="keywords" content="admin template, Chameleon admin template, dashboard template, gradient admin template, responsive admin template, webapp, eCommerce dashboard, analytic dashboard">
    <meta name="author" content="ThemeSelect">
    <title>BR WORK ADMIN TEAM</title>


  </head>
  <body  data-open="click" data-menu="vertical-menu" data-color="bg-gradient-x-purple-blue" data-col="2-columns">

    <!-- fixed-top-->
   


    <div  data-scroll-to-active="true" data-img="{% static 'common/theme-assets/images/backgrounds/02.jpg' %}">
      <div >
        <ul >       
          <li ><a  href="index.html">
              <h3 >Sales Team BR</h3></a></li>
          <li ><a ><i ></i></a></li>
        </ul>
      </div>
      <div >
        <ul  id="main-menu-navigation" data-menu="menu-navigation">
          <li ><a href="/superdashboard"><i ></i><span  data-i18n="">Sales Sheets</span></a>
          </li>
          <li ><a href="/supersearch"><i ></i><span  data-i18n="">Search</span></a>
          </li>
         
        </ul>
      </div><a  href="/supersignout" >Logout</a>
      <div ></div>
    </div>

    <div >
      <div >
        <form method="post">
          {% csrf_token %}
            <label> <p>Search Product By ID</p></label>
            <input type="text" name="search" />
            <button  type ="submit" > Submit </button>
            {% comment %} <button  type ="submit" > Submit </button> {% endcomment %}
            {% if messages %}
            {% for message in messages %}
             <h2> {{message}} </h2>
            {% endfor %}
            {% endif %}
          </form>
        <form method="post">
          {% csrf_token %}
            {% for result2 in sa_storesheet %}
            <label> Product Name </label>
            <input type="text" name="pro_na" value="{{result2.product_name}}" autocomplete="off" />
            <label> Date </label>
            <input type="text" name="date" value="{{result2.date}}" autocomplete="off" />
            <label> Product Cost </label>
            <input type="text" name="pro_cost" value="{{result2.product_cost}}" autocomplete="off" />
            <label> Quantity </label>
            <input type="number" name="qty" value="{{result2.qty}}" autocomplete="off" />
            <label> Sale Price   Shipping </label>
            <input type="number" name="sps" value="{{result2.sale_price_shipping}}" autocomplete="off" />
            <label> COGS </label>
            <input type="number" name="cogs" value="{{result2.cogs}}" autocomplete="off" />
            <label> Gross Profit </label>
            <input type="number" name="gp" value="{{result2.gross_profit}}" autocomplete="off" />
            <label> Amazon Fee </label>
            <input type="number" name="af" value="{{result2.amazon_fee}}" autocomplete="off" />
            <label> Supplier Tax </label>
            <input type="number" name="sup_tax" value="{{result2.supplier_tax}}" autocomplete="off"  />
            <label> Supplier Shipping Cost </label>
            <input type="number" name="ssc" value="{{result2.supplier_shipping_cost}}" autocomplete="off" />
            <label> Amazon Administrative Fee </label>
            <input type="number" name="aaf" value="{{result2.amazon_administrative}}" autocomplete="off" />
            <label> Net Profit </label>
            <input type="number" name="nf" value="{{result2.net_profit}}" autocomplete="off" />
            <label> Befrugal </label>
            <input type="number" name="bf" value="{{result2.befrugal}}" autocomplete="off" />
            <label> Cash Back Profit </label>
            <input type="number" name="cbp" value="{{result2.cash_back_profit}}" autocomplete="off" />
            <label> Total Profit </label>
            <input type="number" name="tp" value="{{result2.total_profit}}" autocomplete="off" />
    
            <label> Ordered </label>
            <select name="order_sta">
              <option disabled="true" selected>{{result2.ordered_status}}</option>
              <option value ="On Process">On Process</option>
              <option value ="Delivered">Delivered</option>
              <option value ="Cancel">Delivered</option>
            </select>
           
            <label> Shipped </label>
            <select name="shipped_sta">
              <option disabled="true" selected>{{result2.shipping_status}}</option>
              <option value ="On Process">On Process</option>
              <option value ="Delivered">Delivered</option>
              <option value ="Cancel">Delivered</option>
            </select>
            <label> Supplier </label>
            <select name="supplier">
              <option disabled="true" selected>{{result2.supplier_name}}</option>
              <option value ="Amazon">Amazon</option>
              <option value ="Wallmart">Wallmart</option>
              <option value ="Etsy">Etsy</option>
              <option value ="Ebay">Ebay</option>
              <option value ="Facebook">Facebook</option>
            </select><br>
            <label> Customer Name </label>
            <input type="text" name="customer" value="{{result2.customer_name}}" autocomplete="off"  />
            <label> Customer Mobile </label>
            <input type="text" name="mobile" value="{{result2.customer_mobile}}" autocomplete="off"  />
            <label> Customer Address </label>
            <input type="text" name="address" value="{{result2.customer_address}}" autocomplete="off"  />
            <label> Customer Email Address </label>
            <input type="text" name="email" value="{{result2.o_a_email_address}}" autocomplete="off"  />
            <label> Order ID </label>
            <input type="text" name="orderID" value="{{result2.purchase_order_id}}" autocomplete="off"  />
            <label> Amazon Order ID </label>
            <input type="text" name="aoi" value="{{result2.amazon_order_id}}" autocomplete="off"  />
            <label> Amazon Tax </label>
            <input type="number" name="ama_tax" value="{{result2.amazon_tax}}" autocomplete="off"  />
            <label> Note </label>
            <input type="text" name="note" value="{{result2.notes}}" autocomplete="off"  />
            <label> Editor </label>
            <input type="text" name="editor" value="{{result2.editor}}" autocomplete="off"  />
            
            <div >
              <input type="submit" name="submit" id="submit"  value="Update Data"/>
            </div>
            {% endfor %}

          </form>
          </div>
        </div>
 

        </div>
      </div>
    </div>




  </body>
</html>

CodePudding user response:

If the user is authenticated, the compiler checks that the request method is POST and that your update request method is always POST. So, the compiler always gets into first if and returns the render. If the request is for search or updates it always returns from the first if statements, it will not enter your second if(update code).

So you must have to change the condition for first if.

  • Related