Receive data from Django front end form - django

what I am trying to do:
I am trying to get data from Django frontend table, and post it back to frontend.
I am not using modelForm, I thought I don't need save the data to database.
My code:
in views.py:
def panelThree(request):
startDate = ''
endDate = ''
byOtherField = ''
if request.method == 'POST':
form = QueryForm(request.POST)
if form.is_valid():
startDate = request.POST.get('date')
endDate = request.POST.get('date1')
byOtherField = request.POST.get('date2')
else:
form = QueryForm()
# if not byOtherField:
dataTable = Production.objects.order_by('-id')
return render(request, 'frontend/panelThree.html', {
'form': form, 'dataTable': dataTable, 'startDate':startDate
})
frontend_form:
<form method="post">
{% csrf_token %}
<div class="row">
<div class="col-md-8">
<div class="row">
<div class="col-md-4">
<label >
TextField1
</label>
<input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
<p> >> Start Date </p>
</div>
<div class="col-md-4">
<label class="control-label " for="date1">
TextField2
</label>
<input class="form-control" id="date1" name="date1" placeholder="MM/DD/YYYY" type="text"/>
<p> >> End Date </p>
</div>
<div class="col-md-4">
<label class="control-label " >
Or
</label>
<label class="control-label " for="date2">
TextField3
</label>
<input class="form-control" id="date2" name="date2" placeholder="MM/DD/YYYY" type="text"/>
<p> >> By Other Field </p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="button-box">
<button class="btn btn-primary " name="submit" type="submit"> Search </button>
</div>
</div>
</div>
</form>
My error:
there is no error, but I just cannot get value from the form post to 'startDate'. It always shows blank value.
I appreciate the help to address this issue, or any ways to work around. Thanks in advance.

2 problems here.
1) The fields in the html are named incorrectly.
<input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
The name field here should be set to startDate, and so should the id field.
2) You're grabbing data directly from POST.
After verifying the form is_valid, you should get data from it like this:
startDate = form.cleaned_data['startDate']

By removing the "if form.is_valid()" statement, and changing back to request.POST.get method, it's working
def panelThree(request):
startDate = ''
endDate = ''
byOtherField = ''
if request.method == 'POST':
form = QueryForm(request.POST)
startDate = request.POST.get('startDate')
endDate = request.POST.get('endDate')
byOtherField = request.POST.get('byOtherField')
else:
form = QueryForm()
# if not byOtherField:
dataTable = Production.objects.order_by('-id')
return render(request, 'frontend/panelThree.html', {
'form': form, 'dataTable': dataTable, 'startDate':startDate
})

Related

Django form is saved but result field is empty in database

Django form is saved but "result" field is showing empty in database.
Even after populating the filed from admin panel, it is saved but it still shows empty.
Models.py
class Result(models.Model):
class Choises(models.TextChoices):
POSITIVE = "POSITIVE", "Positive"
NEGATIVE = "NEGATIVE", "Negative"
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50, default=None)
result = models.CharField(max_length = 100, choices=Choises.choices, blank=False
)
resultDoc = models.ImageField(upload_to='testResults', height_field=None, width_field=None,)
def __str__(self):
return self.user.username
Forms.py
class resultForm(forms.ModelForm):
class Meta:
model = Result
fields = ['name', 'result', 'resultDoc']
views.py
def inputResult(request, pk):
user = User.objects.filter(id=pk).first()
profile = newProfile.objects.filter(user=user).first()
if profile == None:
profile = oldProfile.objects.filter(user=user).first()
rForm = resultForm(request.POST, request.FILES)
if request.method == 'POST':
rForm = resultForm(request.POST, request.FILES)
if rForm.is_valid():
order = rForm.save(commit=False)
order.user_id = pk
order.save()
return redirect('stored_records')
else:
rForm = resultForm()
context = {'user' : user, 'profile':profile, 'rForm': rForm}
return render(request, 'Testing booth End/input-result-customer-info.html', context)
input-result-customer-info.html
<form action="" method = "POST" enctype= "multipart/form-data">
{% csrf_token %}
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Uploaded By/Doctor Name">
</div>
<div class="mb-3">
<label for="result" class="form-label">Result</label>
<select class="form-select" id="result" name="result" class="form-control">
<option value="POSITIVE">Positive</option>
<option value="NEGATIVE">Negative</option>
</select>
</div>
<div class="mb-3">
<label>Upload Scan File</label>
<div class="upload d-flex justify-content-between">
<div class="file-placeholder">Upload Scan File</div>
<input type="file" class="form-control d-none" id="resultDoc" name="resultDoc" >
<label for="resultDoc" class="form-label cam-img"> <img src="{% static 'user/images/Camera.png' %}"> </label>
</div>
</div>
<button class="btn btn-primary w-50 ms-auto d-block h-100" type="submit">Upload</button>
</form>
enter image description here
I think that the reason this doesnt work is that you created a form (rForm) in the backend but then you don't use it in the frontend.
This is how you should render your form in the the frontend:
<form method="post">
{{ rForm.as_p }} # This is the easiest possible implementation
<button type="submit">Submit</button>
</form>
If you want to take control of how the form is rendered, then you have to make sure that the input fields are named in the way that your backend expects. You can do it entirely manually or semi-manually, but your field names have to be set correctly or nothing will work.
Example of typical approach, say in case you have several similar text inputs
{% for field in rForm %}
<label for="{{ field.auto_id }}">{{ field.name }}</label>
<input type="text" name="{{ field.html_name }}" id="{{ field.auto_id }}" />
{% endfor %}
Example of fully hands-on approach
<select class="form-select" id="{{ rForm.result.auto_id }}" name="{{ rForm.result.html_name }}" class="form-control">
<option value="POSITIVE">Positive</option>
<option value="NEGATIVE">Negative</option>
</select>
In order to make sure that the inputs are being parsed correctly, add a print statement in your view to print the POST request:
print("Post Request : ", request.POST)
From there you will be able to see if the result field is being picked up correctly or if it's being ignored. Usually when fields get ignored is because they are not named correctly or sometimes it's because they fail validation.
If the rest of the data is saved correctly and just result is being left out then it's almost for sure an issue with the field name because if the form failed validation it would have aborted the entire operation.
P.S. I just noticed that you select input has the class attribute declared twice

''TypeError at /user expected string or bytes-like object'' on submitting the for in django?

When I submitting the form in django it is giving error ''TypeError at /user expected string or bytes-like object''.
This is my staff models
class staff(models.Model):
id = models.AutoField
name = models.CharField(max_length=250)
role = models.CharField(max_length=250)
salary = models.CharField(max_length=250)
address = models.CharField(max_length=250)
number = models.CharField(max_length=250)
date = models.DateField()
This is my user views.
def user(request):
if request.method == "POST" :
name = request.POST['name']
role = request.POST['role']
salary = request.POST['salary']
address = request.POST['address']
number = request.POST['number']
date = DateTimeField()
ins = staff(name=name, role=role, salary=salary, address=address, date=date, number=number)
ins.save()
staffs = staff.objects.all()
return render(request, "salary/user.html", {'staff': staffs})
and this is form of template user.html
<form class="forms-sample" action="/user" method="post">
{% csrf_token %}
<div class="form-group row">
<label for="exampleInputUsername2" class="col-sm-3 col-form-label">Name:</label>
<div class="col-sm-9">
<input type="text" name="name" id="name" class="form-control" id="exampleInputUsername2" placeholder="Username">
</div>
</div>
<div class="form-group row">
<label for="exampleInputUsername2" class="col-sm-3 col-form-label">Role:</label>
<div class="col-sm-9">
<input type="text" name="role" id="role" class="form-control" id="exampleInputUsername2" placeholder="Role">
</div>
</div>
<div class="form-group row">
<label for="exampleInputUsername2" class="col-sm-3 col-form-label">Salary:</label>
<div class="col-sm-9">
<input type="text" name="salary" id="salary" class="form-control" id="exampleInputUsername2" placeholder="Salary">
</div>
</div>
<div class="form-group row">
<label for="exampleInputUsername2" class="col-sm-3 col-form-label">Address:</label>
<div class="col-sm-9">
<input type="text" name="address" id="address" class="form-control" id="exampleInputUsername2" placeholder="Address">
</div>
</div>
<div class="form-group row">
<label for="exampleInputUsername2" class="col-sm-3 col-form-label">Mobile no.:</label>
<div class="col-sm-9">
<input type="text" name="number" id="number" class="form-control" id="exampleInputUsername2" placeholder="Mobile no.">
</div>
</div>
<button type="submit" class="btn btn-primary mr-2">Submit</button>
<button class="btn btn-dark">Cancel</button>
</form>
I am new in django and i not know what the problem is.
Thanks for helping in advance.
It makes no sense to pass a reference to the AutoField model field in your model, you should construct a field, so:
class staff(models.Model):
id = models.AutoField()
# &vellip;
as for the date field, you can work with auto_now_add=True [Django-doc] to automatically fill in the current day:
class staff(models.Model):
# &vellip;
date = models.DateField(auto_now_add=True)
then this can be omitted while constructing a staff object:
def user(request):
if request.method == "POST" :
name = request.POST['name']
role = request.POST['role']
salary = request.POST['salary']
address = request.POST['address']
number = request.POST['number']
# no date=… &downarrow;
ins = staff.objects.create(name=name, role=role, salary=salary, address=address, number=number)
ins.save()
staffs = staff.objects.all()
return render(request, "salary/user.html", {'staff': staffs})
It might however be better to work with Django forms to validate, clean and fill in data from a POST request.
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model from staff to Staff.

Django Generated Timeslots Selector Required is Always true, How to add value>0

I have a booking system in which i enter dates and timeslots available to book.
the form gets the timeslots from the date and converts it to the user timezone time.
i want the client to select a date and an available timeslot before continuing the form but even with required it doesnt work.
i have a model for timeslots and one for event, date+timeslot
then a form to make client select available date+timeslot, with a html to find timeslot available for each day
html
<option value="">{% if time_slots %}Available Slots{% else %}No Slot Available{% endif %}</option>
{% for time_set in time_slots %}
<option value="{{ time_set.pk }}">{{ time_set.start }} - {{ time_set.end }}</option>
{% endfor %}
models
class TimeSlots(models.Model):
start = models.TimeField(null=True, blank=True)
end = models.TimeField(null=True, blank=True)
class Meta:
ordering = ['start']
def __str__(self):
return '%s - %s' % (self.start.strftime("%I:%M %p"), self.end.strftime("%I:%M %p"))
class Event(models.Model):
event_date = models.DateField()
start = models.ForeignKey(TimeSlots, on_delete=models.CASCADE, verbose_name='Slot Time', null=True)
available = models.BooleanField(default=True)
class Meta:
verbose_name = u'Event'
verbose_name_plural = u'Event'
def __str__(self):
return str(self.event_date)
def get_absolute_url(self):
url = reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=[self.pk])
return u'%s' % (url, str(self.start))
form
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ('patient_name', 'patient_country','phone_number', 'email', 'event_date','start', 'timestamp', 'datestamp')
widgets = {
'event_date': DateInput(),
'patient_country': CountrySelectWidget(),
}
def __init__(self, *args, **kwargs):
super(PatientForm, self).__init__(*args, **kwargs)
self.fields['start'].queryset = TimeSlots.objects.none()
if 'event_date' in self.data:
try:
event_id = self.data.get('event_date')
# event = Event.objects.get(pk=event_id)
self.fields['start'].queryset = TimeSlots.objects.filter(event__event_date=event_id, event__available=True)
except (ValueError, TypeError):
pass # invalid input from the client; ignore and fallback to empty City queryset
elif self.instance.pk:
self.fields['start'].queryset = self.instance.timeslot_set
views
class PatientCreate(CreateView):#was CreateView
form_class = PatientForm
template_name = 'appointment/index.html'
def get_context_data(self, **kwargs): # new
context = super(PatientCreate, self).get_context_data(**kwargs)
context['key'] = settings.STRIPE_PUBLISHABLE_KEY
return context
def load_time_slots(request):
event_date = request.GET.get('event_date')
client_timezone = request.GET.get('timezone')
client_timezone = pytz.timezone(client_timezone)
event_date, original_date = get_original_event_date_by_timezone(client_timezone, event_date)
time_slots = TimeSlots.objects.filter(event__event_date= event_date, event__available=True)
final_time_slots = []
for time_slot in time_slots:
start_time = time_slot.start
original_start_date_time = original_date.replace(hour=start_time.hour, minute=start_time.minute,
second=start_time.second,
tzinfo=original_time_zone)
timezone_start_date_time = original_start_date_time.astimezone(client_timezone)
end_time = time_slot.end
original_end_date_time = original_date.replace(hour=end_time.hour, minute=end_time.minute,
second=end_time.second,
tzinfo=original_time_zone)
timezone_end_date_time = original_end_date_time.astimezone(client_timezone)
final_time_slots.append({'pk': time_slot.pk, 'start': timezone_start_date_time.time,
'end': timezone_end_date_time.time})
return render(request, 'appointment/dropdown_list_options.html', {'time_slots': final_time_slots})
def get_original_event_date_by_timezone(client_timezone, event_date):
client_date = datetime.datetime.strptime(event_date, '%Y-%m-%d')
client_date = client_date.replace(tzinfo=client_timezone)
original_date = client_date.astimezone(original_time_zone)
original_date = original_date.replace(hour=0, minute=0, second=0, microsecond=0)
event_date = original_date.strftime('%Y-%m-%d')
return event_date, original_date
def create_event(request, start_time, day_date):
time_slot = TimeSlots.objects.get(start=start_time)
Event.objects.create(event_date=day_date, start=time_slot)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
form page html
<div class="container" style="margin-top:50px;margin-bottom:50px;">
<div class="stepwizard col-md-offset-3">
<div class="stepwizard-row setup-panel">
<div class="stepwizard-step">
1
<p>Date & Time</p>
</div>
<div class="stepwizard-step">
2
<p>Information</p>
</div>
<div class="stepwizard-step">
3
<p>Calling Method</p>
</div>
<div class="stepwizard-step">
4
<p>Payment Method</p>
</div>
</div>
</div>
<form role="form" action="{% url 'charge' %}" method="POST" id="patientForm" data-times-url="{% url 'ajax_load_time_slots' %}">
<div class="row setup-content" id="step-1">
<div class="col-xs-6 col-md-offset-3">
<div class="col-md-12">
<h3> Appointments date and time</h3>
<div class="form-group">
<label class="control-label" for="id_event_date">Event Date:</label>
<input class="form-control" type="date" name="event_date" id="id_event_date" required="required" />
</div>
<div class="form-group">
<label class="control-label" for="id_start">{% trans "Time:"%}</label>
<p><select required="required" class="form-control" name="start" style="display:inline;" id="id_start">
<option value="">---------</option></select></p><input type="hidden" name="timezone">
<script>$("#patientForm input[name='timezone']").val(Intl.DateTimeFormat().resolvedOptions().timeZone);</script>
</select></p>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Next</button>
</div>
</div>
</div>
<div class="row setup-content" id="step-2">
<div class="col-xs-6 col-md-offset-3">
<div class="col-md-12">
<h3> Step 2</h3>
<div class="form-group">
<label for="id_patient_fname" class="control-label">First Name:</label>
<input name="patient_name" id="id_patient_name" required="required" maxlength="100" type="text" class="form-control" placeholder="Enter First Name" />
</div>
<div class="form-group">
<label class="control-label">Last Name:</label>
<input required="required" maxlength="100" type="text" class="form-control" placeholder="Enter Last Name" />
</div>
<div class="form-group">
<label for="id_phone_number" class="control-label">Phone Number:</label>
<input name="phone_number" id="id_phone_number" required="required" maxlength="100" type="text" class="form-control" placeholder="Enter Phone Number" />
</div>
<div class="form-group">
<label for="id_emal" class="control-label">Email:</label>
<input name="email" id="id_email" maxlength="100" type="text" required="required" class="form-control" placeholder="Enter Email" />
</div>
<div class="form-group">
<label class="control-label">City</label>
<textarea required="required" class="form-control" placeholder="Enter your address"></textarea>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Next</button>
</div>
</div>
</div>
<div class="row setup-content" id="step-3">
<div class="col-xs-6 col-md-offset-3">
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Choose The Way You Want to Receive The Video Call:</label>
<label class="radio-inline"><input class="form-control" type="radio" name="optradio" checked>Skype</label>
<label class="radio-inline"><input class="form-control" type="radio" name="optradio">Whatsapp</label>
</div>
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Next</button>
</div>
</div>
</div>
<div class="row setup-content" id="step-4">
<div class="col-xs-6 col-md-offset-3">
<div class="col-md-12">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_KPSQTmUOl1DLP2eMc7zlvcnS"
data-description="Buying a 30mn Skype Session" data-amount="3000" data-locale="auto"></script>
</div>
</div>
</div>
</form>
</div>
in the html of the form page, i add required to select but it doesnt work
i need client to select lets say 29/01/2019, then if there is availability choose a timeslote lets say 5.30pm-6.00pm, and then only the next arrow will appear
With CreateView it's a little bit tricky when you want to initialize your ModelForm data. So, instead of doing initialization under your ModelForm, do it under the CreateView class like this example:
Your form:
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ('patient_name', 'patient_country','phone_number', 'email', 'event_date','start', 'timestamp', 'datestamp')
widgets = {
'event_date': DateInput(),
'patient_country': CountrySelectWidget(),
}
Your view:
class PatientCreate(CreateView):
form_class = PatientForm
template_name = 'appointment/index.html'
initial = {}
def get_initial(self):
base_initial = super().get_initial() # it's a simple dict
# initialize your form's data here
# Your logic ...
return base_initial
# The rest of your logic
...
And, in order to know why you need to do this. CreateView inherits from FormMixin which has initial and get_initial() thus will initialize your form's data instead of doing it under your form.
See this links for more details: CreateView MRO and FormMixin

Django How do i pass the url parameter with the form button?

Hi i am Newbie in django i read django documentation but i had made a url with parameteres i want to book a car . for booking i need car id and driver id . after redirect to my booking page . my book now button is not sending the driver id and car id . please help me.
for example.
i am having a John as a Driver , and he click on Audi To book . after getting his url i want to save it to my booking database . after click on button in my html "book now" it doesnt render to next page book a car and dont get the car id and driver id.
please help me.
Sorry for my english . if didnt understand please let me know in comments ill more explain to you and share more content . i am just beginner in field so just learning .
Views
#csrf_protect
def rentacar_car(request, car_id,driver_id):
try:
args['car'] = Car.objects.get(id__exact=car_id)
args['driver_id'] = driver_id
except:
args['car'] = None
args['driver_id'] = None
if args['car'] is not None:
template = Template.objects.get(template_default__exact=1)
return render(request, template_page, args)
else:
return HttpResponseRedirect('/rentacar/list/')
def rentacar_booking(request):
template = Template.objects.get(template_default__exact=1)
template_page = template.template_alias + str("/rentacar/rentacar_booking_form.html")
menu_config_list = MenuItemRentacarList.objects.all()[0]
menu_config = MenuItemRentacarList.objects.get(id=menu_config_list.id)
args['main_menu'] = MenuMenu.objects.get(id__exact=template.template_main_menu_id)
args['menu_items'] = MenuItem.objects.filter(
menu_item_menu=args['main_menu'],
menu_item_published=1,
)
args['current_menu_item'] = menu_config.menu_item_rentacar_list_menu_item
all_modules = Module.objects.filter(
module_show_option__exact='all',
module_published=1
)
selected_modules = Module.objects.filter(
module_show_option__exact='selected',
module_published=1,
module_menu_item=args['current_menu_item']
)
excluded_modules = Module.objects.filter(
module_show_option__exact='except',
module_published=1,
).exclude(
module_menu_item=args['current_menu_item']
)
args['modules'] = list(chain(all_modules, selected_modules, excluded_modules))
try:
args['car'] = Car.objects.get(id__exact=request.POST.get('car_id'))
args['driver'] = Driver.objects.get(id__exact=request.POST.get('driver_id'))
except:
args['car'] = None
args['driver'] = None
if args['car'] is not None:
return render(request, template_page, args)
else:
return HttpResponseRedirect('/rentacar/list/')
else:
return HttpResponseRedirect('/')
Templates
rentacar_book_form
<div class="row">
<div class="medium-6 small-12 columns">
<label>
Book Start Date <br>
<input type="date" name="book_booking_start_date" required>
</label>
</div>
<div class="medium-6 small-12 columns">
<label>
Book End Date <br>
<input type="date" name="book_booking_end_date" required>
</label>
</div>
</div>
<hr>
<input type="submit" class='button large primary' value="Book {{ car.car_brand.brand_name }} {{ car.car_name }}">
</div>
</div>
<input type="text" name="book_booking_car_car" value="{{ car.id }}" class="hidden">
<input type="text" name="book_booking_driver_driver" value="{{ driver.id }}" class="hidden">
rentacar_car_car
<form action="/rentacar/book-a-car/" method="post">
{% csrf_token %}
<input name="car_id" type="text" value="{{ car.id }}" class="hidden" required>
<input name="driver_id" type = 'text' value="{{ driver.id }}" class = 'hidden' required>
<button type="submit" class="button primary uppercase centered full-width">
Book now!
</button>
</form>
urls.py
url(r'^rentacar/book-a-car/$', extension_views.rentacar_booking),
url(r'^rentacar/list/(\d+)/$', extension_views.rentacar_list),
url(r'^rentacar/car/(\d+)/driver/(\d+)/$', extension_views.rentacar_car),
If you just want to get the values from hidden inputs just do this.
In method rentacar_car type:
car_id = request.GET.get('car_id')
driver_id = request.GET.get('driver_id')
print(car_id)
print(driver_id)
And since you are getting the values from those inputs you have to change method of form to GET.

ModelForm not passing all parameters

After some great help by #kreigar I was able to start using django's ModelForm instead of form.Forms. Only problem now is that I am not passing the two primary keys to associate my review form :-(.
Currently my view looks like such:
#views.py
#login_required
def wine_review_page(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
review = None
if Review.objects.filter(user=request.user, wine=wine).exists():
review = Review.objects.get(user=request.user, wine=wine)
if request.method == 'POST':
form = WineReviewForm(request.POST, instance=review)
if form.is_valid():
form.save()
return HttpResponseRedirect('/detail/%s/' % wine_id )
else:
form = WineReviewForm(instance=review)
variables = RequestContext(request, {'form': form, 'wine': wine })
return render_to_response('wine_review_page.html', variables)`
and my model form like this:
#models.py
class WineReviewForm(ModelForm):
class Meta:
model = Review
widgets = {
'wine': HiddenInput(),
'user': HiddenInput(),
'like_dislike': HiddenInput(),
'fave': HiddenInput(),
'sweet_dry': HiddenInput(),
'crisp_buttery: HiddenInput(),
'fruity_earthy': HiddenInput(),
'smooth_spicy': HiddenInput(),
'light_robust': HiddenInput(),
'label_rating': HiddenInput()
}
Wine_review_form:
<form method="post" action="/review/{{ wine.id }}/">
{% csrf_token %}
{{form.as_p}}
<div id="form_div" data-role="fieldcontain">
<script>
$('#form_div > input').hide();
</script>
{% if wine.wine_kind == 'whites' %}
<div class="slider_labels">
<span id="sweet_text"class="left_slider_text">Sweet</span>
<span id="dry_text"class="right_slider_text">Dry</span>
</div>
<input type="range" name="sweet_dry" id="sweet_dry_slider" value="50" min="0" max="100" onchange="sweetDryValue(this.value)" />
<div class="slider_labels">
<span id="crisp_text"class="left_slider_text">Crisp</span>
<span id="buttery_text"class="right_slider_text">buttery</span>
</div>
<input type="range" name="crisp_buttery" id="crisp_buttery_slider" value="50" min="0" max="100" onchange="crispButteryValue(this.value)" />
{% else %}
<div class="slider_labels">
<span id="fruity_text"class="left_slider_text">Fruity</span>
<span id="earthy_text"class="right_slider_text">Earthy</span>
</div>
<input type="range" name="fruity_earthy" id="fruity_earthy_slider" value="50" min="0" max="100" onchange="fruityEarthyValue(this.value)" />
<div class="slider_labels">
<span id="smooth_text" class="left_slider_text">Smooth</span>
<span id="spicy_text" class="right_slider_text">Spicy</span>
</div>
<input type="range" name="smooth_spicy" id="smooth_spicy_slider" value="50" min="0" max="100" onchange="smoothSpicyValue(this.value)" />
{% endif %}
<div class="slider_labels">
<span id="light_text"class="left_slider_text">Light</span>
<span id="robust_text" class="right_slider_text">Robust</span>
</div>
<input type="range" name="light_robust" id="light_robust_slider" value="50" min="0" max="100" onchange="lightRobustValue(this.value)" />
<div class="slider_labels">
<span id="sad" class="left_slider_text">Sad</span>
<span id="rad" class="right_slider_text">Rad</span>
<div id="label_rating">Label Rating</div>
</div>
<input type="range" name="label_rating" id="label_rating_slider" value="50" min="0" max="100" onchange="labelRatingValue(this.value)" />
<br>
<br>
<div class="ui-grid-b">
<div class="ui-block-a">
<input type="radio" name="like_dislike" id="like" value="like" />
<label for="like">like</label>
</div>
<div class="ui-block-b">
<input type="radio" name="like_dislike" id="dislike" value="dislike" />
<label for="dislike">dislike</label>
</div>
<div class="ui-block-c">
<input type="checkbox" name="fave" id="checkbox-1" class="custom" />
<label for="checkbox-1">fave</label>
</div>
</div>
</fieldset>
</div>
<input type="submit" value="Judged!" rel="external"/>
When checking my post request the only things I am not passing are the: wine_id and the user_id. This is really confusing.
Am I missing soomething simple?
I looked through documentation and examples but no luck so far.
It actually looks like you wouldn't need your wine_id and user_id (unless you want to allow users to post reviews for other users, or switch wines they are reviewing on the fly).
This should work for you:
#login_required
def wine_review_page(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
if Review.objects.filter(user=request.user, wine=wine).exists():
review = Review.objects.get(user=request.user, wine=wine)
else:
review = Review(user=request.user, wine=wine) #this added
if request.method == 'POST':
form = WineReviewForm(request.POST, instance=review)
if form.is_valid():
form.save()
return HttpResponseRedirect('/detail/%s/' % wine_id )
else:
form = WineReviewForm(instance=review)
variables = RequestContext(request, {'form': form, 'wine': wine })
return render_to_response('wine_review_page.html', variables)
If you do want to allow the user to modify either of those fields, edit your question to show your template or how the users are submitting data, since all your fields are using hiddenInput widgets, Its hard to tell how any data would get into the request.POST