Validation for current user - django

How to realize checking 'name' for current user in forms.py in ValidationError('Same name already added, change name').
views.py
#login_required
def main_page(request):
form = URL_listForm(request.POST)
if request.method == "POST":
if form.is_valid():
name = form.cleaned_data['name']
if URL_list.objects.filter(user=request.user, name=name).exists():
return HttpResponse('Same name already added, change name')
new_post = form.save(commit=False)
new_post.user = request.user
new_post.save()
return HttpResponse("Data added")
return render(request, 'link/main.html', {'form': form})

If you want validate in database
#-------ADDED CODE
data_tmp = """SELECT count(*) from jobtest WHERE link = %s""", (line)
data_tmp = cur.fetchall()
#-------END ADDED CODE
if (data_tmp == 0 ) :
Not exist
add form with name
<input type="text" id="projectName" size="40" placeholder="Spot your project files">
<input type="button" id="spotButton" value="Spot">
when press post button and action to api you can get value in input field using request.form['Name']
if you want send data from server code to html
return render_template('index.html', data=userinfo)
and render as
{% userinfo %}

Related

Unique=True in Form not throwing any error

I'm entering a duplicate value (already saved in another instance of the same model) in my form to test the unique=True attribute. form.is_valid() returns 'False', as expected, but I don't receive any prompt in the template. Shouldn't I get prompted something like "obj with this value already exists"? The page simply reloads... What am I missing?
forms.py
def update_route(request, pk):
instance = Route.objects.get(id=pk)
if request.method == "POST":
form = RouteForm(request.POST)
if form.is_valid():
data = form.cleaned_data
instance.name = data['name']
instance.priority = data['priority']
instance.url = data['url']
return redirect('campaigns:routes_list')
form = RouteForm(instance=instance)
context= {
'form': form,
}
return render(request, "campaigns/route_form.html", context)
models.py
class Route(models.Model):
name = models.CharField(max_length=48)
priority = models.SmallIntegerField(choices=PRIORITY_LEVEL, default=0, unique=True)
url = models.URLField()
Template
<form method="post" action="">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
Your update_route() view handles the condition in which the submitted form is valid (form.is_valid()), but not the condition in which the form is invalid.
The errors you are looking for are stored in the form object that you created with RouteForm(request.POST). The errors are generated when the is_valid() method is called.
This form object needs to be added to the context dict and rerendered to the user for the errors to surface. But your code currently overwrites that object with form = RouteForm(instance=instance), so the POST data and the related errors disappear.
One solution could be to handle it in the conditional statement:
if form.is_valid():
...
else:
context = {'form': form}
return render(request, "campaigns/route_form.html", context)
Another solution could be to create a conditional statement for GET requests, for example:
elif request.method == 'GET':
form = RouteForm(instance=instance)

Django - Problems with Ajax Form [duplicate]

I want to use Ajax in Django to handle the view of my checkout form after it has been submitted. After the form is submitted, I want it to go to :
return HttpResponseRedirect(reverse(str(next_page))+"?address_added=True") , i.e http://127.0.0.1:8000/checkout/?address_added=True
But for some reason, it is not going there. Rather it's being going to http://127.0.0.1:8000/checkout/?csrfmiddlewaretoken=W4iXFaxwpdtbZLyVI0ov8Uw7KWOM8Ix5GcOQ4k3Ve65KPkJwPUKyBVcE1IjL3GHa&address=123+Main+Street&address2=&state=MA&country=USA&zipcode=55525&phone=%28877%29+314-0742&billing=on
As a result, the form data is also not getting saved. I was thinking if it were because of the new version of Django.
What I want to do is that after they submit the place order button, the form is going to be None, i.e disappear and then I would add a credit card form there for payment. But it is not happening. What is wrong here? How can I do this or is there a better way to do this?
My forms.py:
class UserAddressForm(forms.ModelForm):
class Meta:
model = UserAddress
fields = ["address", "address", "address2", "state", "country", "zipcode", "phone", "billing"]
My accounts.views.py:
def add_user_address(request):
try:
next_page = request.GET.get("next")
except:
next_page = None
if request.method == "POST":
form = UserAddressForm(request.POST)
if form.is_valid():
new_address = form.save(commit=False)
new_address.user = request.user
new_address.save()
if next_page is not None:
return HttpResponseRedirect(reverse(str(next_page))+"?address_added=True")
else:
raise Http404
My orders.views.py:
#login_required()
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return redirect(reverse("myshop-home"))
try:
new_order = Order.objects.get(cart=cart)
except Order.DoesNotExist:
new_order = Order(cart=cart)
new_order.cart = cart
new_order.user = request.user
new_order.order_id = id_generator()
new_order.save()
except:
return redirect(reverse("cart"))
try:
address_added = request.GET.get("address_added")
except:
address_added = None
if address_added is None:
address_form = UserAddressForm()
else:
address_form = None
if new_order.status == "Finished":
#cart.delete()
del request.session['cart_id']
del request.session['items_total']
return redirect(reverse("cart"))
context = {"address_form": address_form, "cart": cart}
template = "orders/checkout.html"
return render(request, template, context)
My urls.py:
path('ajax/add_user_address', accounts_views.add_user_address, name='ajax_add_user_address'),
My checkout.html:
<form method="POST" action="{% url 'ajax_add_user_address' %}?redirect=checkout">
{% csrf_token %}
<fieldset class="form-group">
{{ address_form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-dark" type="submit">Place Order</button>
</div>
</form>
I would personally split these up in two views, because they do different stuff.
But, if you want to keep it that way, you can do the following:
First of all, because you are making an AJAX Request, you should return a JsonResponse object.
In your view you can render the checkout.html and pass it as a context variable to your json response:
def add_user_address(request):
...
data = dict()
context = {
'address_form': form,
...
}
data['html_form'] = render_to_string("checkout.html",
context,
request=request)
return JsonResponse(data)
And in your $.ajax success function you can do the following
success: function(data) {
// console.log(data);
$("div-to-replace-html").html(data.html_form);
}

The view ___ didn't return an HttpResponse object. It returned None instead

I have a scheduling app where patients can register for appointments. When I submit the form for a new appointment, I get the value error.
views.py
def patient_portal(request):
appointments = Appointment.objects.filter(patient=request.user.patient.pid)
data_input = request.GET.get('date')
selected_date = Appointment.objects.filter(date = data_input).values_list('timeslot', flat=True)
available_appointments = [(value, time) for value, time in Appointment.TIMESLOT_LIST if value not in selected_date]
doctor = Patient.objects.get(doctor=request.user.patient.doctor).doctor
print(doctor)
if request.method == 'POST':
form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
if form.is_valid():
form.save()
return redirect('../home/')
else:
form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
return render(request, 'scheduling/patient.html', {"form" : form, "appointments" : appointments, "available_appointments" : available_appointments, "data_input": data_input, "doctor": doctor})
patient.html:
<form method="post" action="" id="timeslot" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
forms.py:
class AppointmentForm(forms.ModelForm):
class Meta:
model = Appointment
fields = ('doctor','patient','date','timeslot')
Your view must always return an HTTP response. At the moment, your code doesn't handle the case when request.method == 'POST', but the form is invalid.
You can fix your code by de-indenting the final return statement, to move it outside of the else block:
def patient_portal(request):
...
if request.method == 'POST':
form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
if form.is_valid():
form.save()
return redirect('../home/')
else:
form = AppointmentForm(initial={'doctor': doctor,'patient': request.user.patient}, instance=request.user.patient)
return render(request, 'scheduling/patient.html', {"form" : form, "appointments" : appointments, "available_appointments" : available_appointments, "data_input": data_input, "doctor": doctor})

How to retain select option value after submit in django?

I have created select tag with four option values(1,2,3,4).When I select 4 and press submit it changes back to 1.Select option contains quantity of the product needed for the user.So how to retain option value after pressing submit button.I tried like this,after I press submit button value changes back to 1.Is there any way to overcome this problem?
My template file,
<label for="quantity">Quantity</label>
<select id="quantity" name="quantity">
<option value="1" {% if quantity == '1' %}selected{% endif %}>1</option>
<option value="2" {% if quantity == '2' %}selected{% endif %}>2</option>
<option value="3" {% if quantity == '3' %}selected{% endif %}>3</option>
<option value="4" {% if quantity == '4' %}selected{% endif %}>4</option>
</select>
<input type="submit" value="Buy"/>
UPDATE:
forms.py,
class SortForm(forms.Form):
RELEVANCE_CHOICES = (
(1,'1'),(2, '2'),(3,'3'), (4,'4'),(5,'5'),
)
sort = forms.ChoiceField(choices = RELEVANCE_CHOICES,label='Quantity')
views.py,
from .forms import SortForm
#csrf_protect
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products}
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
"form": form
}
return render(request,"buy_book.html",context)
In the template file I added this line,
{{form.as_p}}
Now I am getting blank output.I think the form is not recognising in the template.
The problem here that your template is just showing data, it doesn't know anything about state. So if you want to achieve this kind of behavior you need to supply all required data from backend. Also as #solarissmoke mentioned you should use django forms.
for exmaple(Pseudo code below)
def my_view(request):
if request.method == 'POST':
form = MyForm(request.data)
if form.is_valid():
form.save()
redirect(reverse('myview'))
else:
form = MyForm(instance) # <- instance is object with previously saved data
return render(request, 'my_template.html' , {'form': form})
Second Part
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products}
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
"form": form # <- here is problem
}
return render(request,"buy_book.html",context)
You are adding form to context inside if request.method == 'POST'. It should like this
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products, 'form': form} # <- here
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
}
return render(request,"buy_book.html",context)
In your view, you only ever add the form to the context data if there is a quantity in the post data, you should add this to the context regardless since it is required for your view.
You should also actually use the form, so instead of checking the post data, check the form for validity and then use its cleaned data.
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products,
'form': form}
if form.is_valid():
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = form.cleaned_data.get('sort', 0)
Quantity=int(u_quantity)
total = rate*Quantity
context['total'] = total
return render(request,"buy_book.html",context)
'int' object is not iterable
Probably because your sort field isn't a list of tuples
choices = [(i, i) for i in range(1,6)]

edit the form is not working

Merry Christmas!!!!I work with flask sqlalchemy and wtf. I could create new and would display in info.html But when I try to edit the form there is no changes in database So it is not working. So I wonder where is issues?
app.py
#With this route I can add new form in database
#app.route('/contact', methods=['GET', 'POST'])
def contact():
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate()== True:
contact = Contacts()
# populate the model
form.populate_obj(contact)
db.session.add(contact)
db.session.commit()
# Contact commited to database
# RETURN TO INFO.HTML
return redirect(url_for('info'))
else:
#If the form does not have all fields that are required
return render_template('contact.html', form=form)
# This is the part for edit which is not working
# so I query and populate it but no change none in
# database none in info.html
#app.route('/edit/<int:id>', methods=['POST'])
def edit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate_on_submit()== True:
#populate it
form.populate_obj(user)
db.session.commit()
return redirect(url_for('info'))
else:
#If the form does not have all fields that are required
return render_template('edit.html', id=id )
#app.route('/edit/<int:id>', methods=['GET'])
def profile(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form, obj=user)
return render_template('edit.html',form=form, id =id)
# this route to html that should show all info
#app.route('/info', methods=['GET', 'POST'])
def info():
#query all
info = Contacts.query.all()
return render_template('info.html', contact=info)
model.py
# model with table name Contacts
class Contacts(db.Model):
__tablename__ = "Contacts"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
age = db.Column(db.Integer)
form.py
# this is the form wtf
Class LoginForm(Form):
name = StringField("Name", [validators.Required("Please enter your name.")])
email = StringField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter valid email address.")])
age = IntegerField("age", [validators.Required("Please enter your age.")])
submit = SubmitField("Submit")
info.html
# it should display all updated form But it wont??
{% extends "layout.html" %}
{% block content %}
<h2>show the info</h2>
{% for contact in contact %} # Maybe this is also Problem?
<strong>name:</strong> {{ contact.name}} <br>
<strong>email:</strong> {{ contact.email }} <br>
<strong>age:</strong> {{ contact.age}} <br>
<br>
{% endfor %}
{% endblock %}
You don't have to readd a Object, when you get it per query. The Queryobject is bound to the session. So you only need to commit the changes and not to add them again.
so this should be the corrected code snippet
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
# check the validate and then populate the obj
if form.validate()== True:
# populate it
form.populate_obj(user)
db.session.commit()
I tried your code on my machine. With little modification in code I was able to update database.
Please find updated chunk below:
#app.route('/edit/<int:id>', methods=['POST'])
def submit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
#return render_template('edit.html', form=form, id=1 )
if form.validate() == True:
form.populate_obj(user)
db.session.commit()
return redirect(url_for('info'))
else:
return redirect(url_for(edit, id=id))
#app.route('/edit/<int:id>', methods=['GET'])
def edit(id=None):
user = Contacts.query.get_or_404(id)
form = LoginForm(request.form,obj=user)
return render_template('edit.html', form=form, id=id )
I know this is an older question, but I'm referencing from Why doesn't my WTForms-JSON form update correctly?.
Essentially, you need to initialize the Form first then check to see if it was submitted or not:
form = FormBuilder()
if not form.is_submitted():
form = FormBuilder.populate_obj(obj)
else:
form = FormBuilder() # will populate from submitted data
if form.validate_on_submit():
It helped me when having the same issues.