Thursday, May 30, 2019

problem with form

Hi

I'm trying to make a simple crud applicatio and learn from it. I wonder why my form can't save the data. Here cames my view:

from django.shortcuts import render
from saeed.forms import SmodelForm  
from saeed.models import Smodel
from django.shortcuts import render, redirect  



# Create your views here.
def emp(request):  
    if request.method == "POST":  
        form = SmodelForm(request.POST)  
        if form.is_valid():  
            try:  
                form.save()  
                return redirect('/show')  
            except  AssertionError as error:
                
                print(error)
                pass

     
                

                
    else:  
        form = SmodelForm()  
    return render(request,'saeed/index.html',{'form':form})  
#----------------------------------------
def show(request):  
    smodels = Smodel.objects.all()  
    return render(request,'saeed/show.html',{'smodels':smodels}) 
#----------------------------------------

def edit(request, id):  
    smodels = Smodel.objects.get(id=id)  
    return render(request,'saeed/edit.html', {'smodels':smodels})  
#-------------------------------------------

def update(request, id):  
    smodels = Smodel.objects.get(id=id)  
    form = SmodelForm(request.POST, instance = smodels)  
    if form.is_valid():  
        form.save()  
        return redirect("/show")  
    return render(request,'saeed/edit.html',{'smodels':smodels})  

#---------------------------------------------
def destroy(request, id):  
    smodels = Smodel.objects.get(id=id)  
    smodels.delete()  
    return redirect("/show")  






edit.html

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Index</title>  
    {% load staticfiles %}
     {% load bootstrap4 %}
    <link rel="stylesheet" href="{% static 'css/style.css' %}"/>  
</head>  
<body>  
<form method="POST" class="post-form" action="/update/{{employee.id}}">  
        {% csrf_token %}  
    <div class="container">  
<br>  
    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <h3>Update Details</h3>  
    </div>  
  </div> 
        
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Id:</label>  
    <div class="col-sm-4">  
        <input type="number" name="eid" id="id_eid" required maxlength="8" value="{{ saeed.eid }}"/>  
    </div>  
  </div>  
  <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Name:</label>  
    <div class="col-sm-4">  
        <input type="text" name="elogin" id="id_elogin" required maxlength="8" value="{{ saeed.elogin }}" />  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Password:</label>  
    <div class="col-sm-4">  
        <input type="email" name="epassword" id="id_password" required maxlength="254" value="{{ saeed.password }}" />  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">Like A Day:</label>  
    <div class="col-sm-4">  
        <input type="number" name="elikeday" id="id_elikeday" required maxlength="999" value="{{ saeed.elikeday}}" />  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <button type="submit" class="btn btn-success">Update</button>  
    </div>  
  </div> 
        
        <div class="form-group row">  
    <label class="col-sm-2 col-form-label">Follow per Day:</label>  
    <div class="col-sm-4">  
        <input type="number" name="efollowperday" id="id_efollowperday" required maxlength="999" value="{{ saeed.efollowperday}}" />  
    </div> 
    </div>  
</form>  
</body>  
</html>  


index.html
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Index</title>  
    {% load staticfiles %} 
    {% load bootstrap4 %}
    <link rel="stylesheet" href="{% static 'css/style.css' %}"/>  
</head> 
 
<body>  
<form method="POST" class="post-form" action="/emp">  
        {% csrf_token %}  
    <div class="container">  
<br>  
    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <h3>Enter Details</h3>  
    </div>  
  </div>  
  <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Id:</label>  
    <div class="col-sm-4">  
      {{ form.eid }}  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Login:</label>  
    <div class="col-sm-4">  
      {{ form.elogin }}  
    </div>  
  </div>  
        <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User password:</label>  
    <div class="col-sm-4">  
      {{ form.epassword }}  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">Lika A day:</label>  
    <div class="col-sm-4">  
      {{ form.elikeDay }}  
    </div>  
  </div>
        
 <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User follow per day:</label>  
    <div class="col-sm-4">  
      {{ form.efollowPerDay }}  
    </div>  
  </div>
        

    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <button type="submit" class="btn btn-primary">Submit</button>  
    </div>  
  </div>  
    </div>  
</form>  
</body>  
</html>



show.html

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Index</title>  
    {% load staticfiles %} 
    {% load bootstrap4 %}
    <link rel="stylesheet" href="{% static 'css/style.css' %}"/>  
</head> 
 
<body>  
<form method="POST" class="post-form" action="/emp">  
        {% csrf_token %}  
    <div class="container">  
<br>  
    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <h3>Enter Details</h3>  
    </div>  
  </div>  
  <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Id:</label>  
    <div class="col-sm-4">  
      {{ form.eid }}  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User Login:</label>  
    <div class="col-sm-4">  
      {{ form.elogin }}  
    </div>  
  </div>  
        <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User password:</label>  
    <div class="col-sm-4">  
      {{ form.epassword }}  
    </div>  
  </div>  
    <div class="form-group row">  
    <label class="col-sm-2 col-form-label">Lika A day:</label>  
    <div class="col-sm-4">  
      {{ form.elikeDay }}  
    </div>  
  </div>
        
 <div class="form-group row">  
    <label class="col-sm-2 col-form-label">User follow per day:</label>  
    <div class="col-sm-4">  
      {{ form.efollowPerDay }}  
    </div>  
  </div>
        

    <div class="form-group row">  
    <label class="col-sm-1 col-form-label"></label>  
    <div class="col-sm-4">  
    <button type="submit" class="btn btn-primary">Submit</button>  
    </div>  
  </div>  
    </div>  
</form>  
</body>  
</html>


forms.py

from django import forms  
from saeed.models import Smodel 

class SmodelForm(forms.ModelForm):  
    class Meta:  
        model = Smodel  
        fields = "__all__"  


models:

from django.db import models
from django.conf import settings


# Create your models here.
class Smodel(models.Model):

    eid=models.AutoField(primary_key=True)
   # eid=models.IntegerField(default=0)
    elogin = models.CharField(max_length=8) 
    epassword= models.CharField(max_length=8) 
  
    elikeDay=models.IntegerField(default=10)
    efollowPerDay=models.IntegerField(default=10)
    

   
    #esession = models.TextField()
    class Meta:  
        db_table = "saeed"  


urls.py

"""
Definition of urls for bot3.
"""
from django.conf.urls import include, url
from django.contrib import admin  
from django.urls import path  
from saeed import views  

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = [
    
  # path('', admin.site.urls, name='admin'),
     path('', views.show, name='index'),
   path('', views.emp, name='emp'),
  path('', views.show, name='show'),
    path('', views.show, name='index'),

   # path('', views.emp, name='show'),
   # path('', views.emp, name='index'),

  # path('index', views.show), 
   # path('admin/', admin.site.urls),  
  path('emp', views.emp),  
    path('show',views.show), 
   # path('index', views.emp), 
    

    path('edit/<int:id>', views.edit, name='edit'), 
    path('update/<int:id>', views.update, name='update'),  
    path('delete/<int:id>', views.destroy,  name='delete'),
]



and here cames the complete app:



can you please inform me where is wrong?

thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/725917c1-b714-4bd3-b2e0-ee9271e8fee3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment