sorry for the unreadable code!
I have cleaned it up as suggested and posted it below.
the user is displayed only email,subject,help_topic and message fields. rest all the fields are hidden. when the form is submitted; in the submit_ticket() in views.py i have copied the request.POST into a dictionary and have overwritten the blank values which were in the POST request with dynamically generated values.
interestingly, The form validates now but upon save i get blank values in the database, i.e. values for all the fields are blank!
Please help me out.
P.S. earlier the form was not getting validated due to reasons i am ignorant with
i am attaching the submit function in the views.py file and the forms.py file
forms.py file
from django import forms
from django.contrib.auth.models import User
from ac.models import UserProfile, Category
import datetime
from ac.models import *
from datetime import timedelta
class SubmitTicketForm(forms.ModelForm):
email=forms.EmailField(max_length=128,help_text="please enter your email address")
ticket_id=forms.IntegerField(widget=forms.HiddenInput())
subject=forms.CharField(max_length=100,help_text="subject")
help_topic=forms.ChoiceField(choices=[(x['category'], str(x['category'])) for x in Category.objects.values('category')],help_text="please enter the category of your problem")
message=forms.CharField(max_length=500,help_text="message")
created_date_time=forms.DateTimeField(widget=forms.HiddenInput())
overdue_date_time=forms.DateTimeField(widget=forms.HiddenInput())
closed_date_time=forms.DateTimeField(widget=forms.HiddenInput())
status=forms.IntegerField(widget=forms.HiddenInput())
reopened_date_time=forms.DateTimeField(widget=forms.HiddenInput())
topic_priority=forms.IntegerField(widget=forms.HiddenInput())
duration_for_reply=forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model=Ticket#all the fields are included
fields=('email','subject','help_topic','message')
view where the form is handled
def submit(request):
context=RequestContext(request)
if(request.method=="POST"):
data = request.POST.copy()
print "\n"
print request.user.email
data['created_date_time']=datetime.datetime.now()
data['overdue_date_time']=datetime.datetime.now()
Date=datetime.datetime.now()
Enddate=Date+datetime.timedelta(days=1)
data['overdue_date_time']=Enddate
data['closed_date_time']=Enddate
data['status']=0 # 0 means that the ticket is still open and not yet answered
data['reopened_date_time']=Enddate
data['topic_priority']=2 # 2 is the default priority of medium
data['duration_for_reply']=24 #in hours
if request.user.is_authenticated():
print request.user
data['user_id']=request.user.email
else:
return HttpResponse("you need to be a valid user to submit a ticket. click <a href=''>here</a> to go to the login page")t
from django.db.models import Max
last_ticket=int(Ticket.objects.all().aggregate(Max('ticket_id'))['ticket_id__max'])
data['ticket_id']=last_ticket+1
user_form=SubmitTicketForm(data)
if user_form.is_valid():
user_form.save()
return HttpResponse("Saved successfully")
else:
return HttpResponse("Saved unsuccessfully")
print user_form.errors
else:
user_form=SubmitTicketForm()
return render_to_response(
'submit.html',
{'user_form': user_form},
context)
the model upon which the form is based
class Ticket(models.Model):
user_id=models.EmailField()
topic_id=models.CharField(max_length=100,help_text="enter topic id")
message=models.TextField()
ticket_id=models.IntegerField()
file_uploads=models.FileField(upload_to='tickets/file' , blank=True)
created_date_time=models.DateTimeField(auto_now_add=True)
overdue_date_time=models.DateTimeField(auto_now_add=True)
closed_date_time=models.DateTimeField(auto_now_add=True)
status=models.IntegerField()
reopened_date_time=models.DateTimeField(auto_now_add=True)
topic_priority=models.IntegerField()
duration_for_reply=models.IntegerField()
def __unicode__(self):
return unicode(self.user_id)