Adding extra path to django get_full_path() through form - django

I just want to add something to mypath like: bla1/bla2/bla3/something
or whatever pass in member!
how can i do this?
def to(request):
if request.method == "POST":
x = request.POST['username']
member = whome(unique_user=x)
member.save()
links.objects.create(path=request.get_full_path())
links().save()
return render(request, "home.html")

Related

Django: change TextField before save

This is my model :
class Card(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
imp_id = models.TextField(null = True)
And here is my view :
def Add(request):
if request.method == 'POST':
form = Add_card(request.POST)
if form.is_valid():
save = form.save(commit = False)
save.user = request.user
save.imp_id = "asd" # I tried to change it here but I failed
save.save()
else:
form = Add_card()
cards = Card.objects.all()
return render(request, 'addcard.html', {'form': form, 'cards' : cards})
How can I change that textfield before save?
you can do it like this
def Add(request):
if request.method == 'POST':
request.POST.imp_id="asd"
form = Add_card(request.POST)
if form.is_valid():
save = form.save(commit = False)
save.user = request.user
save.save()
else:
form = Add_card()
cards = Card.objects.all()
return render(request, 'addcard.html', {'form': form, 'cards' : cards})
The problem could be solved by using default='asd'.

Form values are not overwritten when changed - they are stuck at their default values

I have the following view:
#login_required
def my_view(request):
instance = my_model(user=request.user)
form = my_model_form(request.POST,instance = instance)
if request.method == "POST":
if form.is_valid():
form.save(commit=False)
#Field1 and field2 is already in the form (its the input)
# Do some back-end operations to get values for the remaining fields
df = some_util_function()
form.field3 = df["field3"]
form.field4 = df["field4"]
form.field5= df["field5"]
form.save()
return redirect("my_html")
else:
form = my_model_form()
context = {
"form":form
}
return render(request, "discounttracker/my_html.html",context=context)
and the problem is that field3,field4,field5 are not changed. I have even tried to hard-code them to 1000, 2000,3000 (they are FloatField(default=0)) but they remain at their default value when written to the DB.
What am I doing wrong here?
You are setting the attributes of the form, not of the instance wrapped in the form. You should alter this to:
#login_required
def my_view(request):
instance = my_model(user=request.user)
if request.method == 'POST':
form = my_model_form(request.POST, instance = instance)
if form.is_valid():
# Field1 and field2 is already in the form (its the input)
# Do some back-end operations to get values for the remaining fields
df = some_util_function()
# ↓ the instance of the form
form.instance.field3 = df['field3']
form.instance.field4 = df['field4']
form.instance.field5 = df['field5']
form.save()
return redirect("my_html")
else:
form = my_model_form()
context = {
'form': form
}
return render(request, 'discounttracker/my_html.html', context=context)

Save object data in list and use in parameter

I need to use the object details of obj to save in a list or some sort to use as a parameter in my send_order_verification() function.
def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
obj = form
obj.save()
send_order_verification(order_details, obj.email)
This is my send_order_verification() function:
def send_order_verification(order_details, email):
return send_mail(
('Thank you for your order #{0}', x),
('Name: {0}\nEmail: {1}\nTelephone: {2}', x, y, z),
[settings.EMAIL_SEND_TO],
['{0}' % email]
)
How could this be done? I tried with order_details = [obj.name, obj.email, obj.telephone] but can't access it with order_details[0] and so forth.
Send the object itself:
def order(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
obj = form
obj.save()
send_order_verification(obj)
def send_order_verification(order_details):
return send_mail(
('Thank you for your order #{0}', order_details.name),
('Name: {0}\nEmail: {1}\nTelephone: {2}', order_details.name, order_details.email, order_details.telefone),
[settings.EMAIL_SEND_TO],
['{0}' % order_details.email]
)

local variable 'context' referenced before assignment

I need get data from my form and treat them
def parsurl(request):
if request.method == 'POST':
form = DomainForm(request.POST)
context = { 'form': form, }
if form.is_valid():
r = form.cleaned_data.get('url_text')
masall =parser_d(r)
for mas1 in masall:
try:
Events.objects.create(events_title=mas1[1], events_text = mas1[2],events_img = mas1[0], events_is_pars=1, events_seourl = "132")
except:
print ("lololo")
else:
form = DomainForm()
return render_to_response(request, 'form.html', context)
Exception Value:
local variable 'context' referenced before assignment
You have defined context in the if request.method == 'POST' branch, but not in the else branch. Therefore you get an error for GET requests.
You could fix it by moving the line outside of the if statement as follows:
if request.method == 'POST':
...
else:
...
context = {'form': form}

Why my code does not show validation form error message?

Why my code does not show validation form error message?
I try something like this:
def index(request):
if request.method == "POST":
if request.POST['form-type'] == 'contact-form':
form = AngebotForm(None, request.POST)
if form.is_valid():
form.save()
msg = 'Good!'
return render_to_response('index.html',{'msg':msg}, context_instance=RequestContext(request))
else:
form = AngebotForm()
else:
form = MessageForm(request.POST)
if form.is_valid():
form.save()
msg = 'Good!'
return render_to_response('index.html',{'msg':msg},context_instance=RequestContext(request))
else:
form = MessageForm()
return render_to_response('index.html',{'a_form':AngebotForm(), 'm_form':MessageForm()},context_instance=RequestContext(request))
What am I doing wrong?
Because in else part you are re-initializing the form which will loose the current state of form.
Just remove these lines:
else:
form = MessageForm()
In the end your view should look like this:
def index(request):
form = AngebotForm()
m_form = MessageForm()
if request.method == "POST":
if request.POST['form-type'] == 'contact-form':
form = AngebotForm(None, request.POST)
if form.is_valid():
form.save()
msg = 'Good!'
return render_to_response('index.html',{'msg':msg}, context_instance=RequestContext(request))
else:
m_form = MessageForm(request.POST)
if m_form.is_valid():
m_form.save()
msg = 'Good!'
return render_to_response('index.html',{'msg':msg},context_instance=RequestContext(request))
return render_to_response('index.html',{'a_form':form, 'm_form':m_form},context_instance=RequestContext(request))