How can I write session variable from model/table in Django? - python

I am trying to assign a session variable based on a model or database table on my Django site. In other words, on the first use, I want the user to select a county from a dropdown list and write the name of that county or pk to a session variable. From therek, that data gets pulled into context_processor before hitting the template. Right now, the error I'm getting is
(1062, "Duplicate entry '1' for key 'county_id'")
I'm not sure if it is trying to write multiple entries into the database, but I don't really want anything databased as you would other data. I just want a session variable stored. I am sure my problem is my view, but I can't seem to get it right. In case you couldn't tell, I'm pretty new at this.
I have a model.py defined that accesses another table of counties.
class NonUserCounty(models.Mdel):
county = models.ForeignKey(County)
def __unicode__(self):
return self.county
I have defined a form.py
class NonUserCountyForm(forms.ModelForm):
class Meta:
model = NonUserCounty
And a views.py
def Welcome(request):
if request.method == 'POST':
form = NonUserCountyForm(request.POST)
if form.is_valid():
mycounty = form.cleaned_data['county']
request.session['mycounty'] = mycounty
form.save()
return HttpResponseRedirect('/')
else:
form = NonUserCountyForm()
context = {'form': form}
return render_to_response('welcome.html', context, context_instance=RequestContext(request))
A Context processor
def mynews(request):
now = datetime.now()
if not request.user.is_authenticated():
if not "mycounty" in request.GET:
MyNews = News
county = County
else:
return HttpResponseRedirect('/welcome/')
else:
user = request.user.get_profile()
county = user.county.all()
MyNews = News
And my template....
<form action="" method="post">
{% csrf_token %}
{{ form }}
<p><input type="submit" alt="register" value="Sign Up" /></p>

Why are you calling form.save() if you don't want to save anything in the database? Just remove that line and it should work.

Related

Django : Form Successful but image not uploaded

MODELS.PY
class Campaign(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
campaign_image = models.ImageField(default="profilepic.jpg",upload_to="campaign_pictures")
FORMS.PY
class RaiseFundsFrom3(forms.ModelForm):
class Meta:
model = Campaign
fields = ['campaign_image']
VIEWS.PY
#login_required
def raise_funds_medical_3(request):
if request.method == 'POST':
form = RaiseFundsFrom3(request.POST, request.FILES or None, instance=request.user)
if form.is_valid():
check = form.save(commit=False)
check.save()
return HttpResponse('form worked')
else:
form = RaiseFundsFrom3()
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
URLS.PY
path('raise/medical/photo', views.raise_funds_medical_3, name="raise_funds_medical_3"),
raise_funds_medical_3.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group pt-2">
<small>Photo formats must be PNG / JPG / JPEG</small>
<input type="file" name="campaign_image" />
</div>
<button class="btn btn-lg button_bfg_blue" type="submit"> <small><b> NEXT </b></small> </button>
</form>
on form submit, i do not get any error, but image is not uploaded to the required folder.
however, in the raise_funds_medical_3 function within views.py, if i remove instance=request.user, the image gets uploaded but i get following error : NOT NULL constraint failed: funds_campaign.user_id
Your form is a ModelForm for a Campaign, so its instance needs to be a Campaign. Don't assign request.user as its instance!
Now, your form isn't including the user field which is required to save a Campaign, so you should assign that yourself in the view before saving to the database:
campaign = form.save(commit=False) # this gives your the form's instance
campaign.user = request.user # this assigns the user
campaign.save() # this commits to the database
Also you should handle the case where the form isn't valid. This is quite simple, just un-indent the last return in your view function, so that return render(...) is also called in case the form isn't valid.
Finally, instead of returning a response when the form is valid, it's good practice to redirect to another view. This way, when the user refreshes the page, the form isn't submitted again. Your final code should look like this:
#login_required
def raise_funds_medical_3(request):
if request.method == 'POST':
form = RaiseFundsFrom3(request.POST, request.FILES or None)
if form.is_valid():
check = form.save(commit=False)
check.user = request.user
check.save()
return redirect(<url_pattern>)
else:
form = RaiseFundsFrom3()
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
Supplementary answer to dirkgroten's one
I have come to completely hate the conventional structuring of a Django Function-based View. They can be re-factored by inverting the validity test and adding one line so that one and only one instantiation of a form is present. The result is IMO far easier to read, and easily generalizes for a view displaying two or more forms.
def raise_funds_medical_3(request):
args = [request.POST, request.FILES or None] if request.method == "POST" else []
form = RaiseFundsFrom3(*args)
if request.method != "POST" or not form.is_valid():
# unbound form or form not valid
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
# form is valid so do the processing and redirect
check = form.save(commit=False)
check.user = request.user
check.save()
return redirect(<url_pattern>)
If you want to process >1 form, the test becomes
if request.method != "POST" or any(
[ not form.is_valid(), not form2.is_valid(), ...]):
which forces evaluation of .is_valid() for all forms, even if the first was not valid, so that all the error messages are shown to the user.
In a complex business application, the processing of a successful form submission may be quite a few more lines of code than this simple example. Having it at the end, not indented, isolated from all the boilerplate save the return redirect(...), makes things much easier!

Django ModelFormSet clicking "Submit" saves form but does not update to database

Background:
I'm building a personal dictionary web-application, and have a queryset of terms and definitions. In my web app, I have an edit page, where I want to show a ModelFormSet that allows the user to edit any/all entries, and delete them if needed - the form has a delete button for each row and a submit button to save changes to the form.
The current issue is that when I click on a set to edit, the formset shows up correctly, and when I change a term and hit "Submit" the form updates to show the change. However, when I go back to my "View Set" page, the term hasn't been updated, which I assume means the change didn't go through to the actual database - the form itself was simply submitted. This is what I would like to currently fix, and I also want to make it so that the delete button deletes the entry.
What I've Tried:
I've gone through every StackOverflow question I could find relating to the topic, and various solutions were: add an instance parameter when passing in "request.POST", re-initialize the formset after saving, change the "action" url in the HTML page, etc., but every solution I try either results in another error or doesn't change anything.
I also checked the documentation but the examples provided were for simpler examples, and I'm not sure where exactly my error is in this case.
Finally, I also used {{ form.errors }} to see if the forms were being validated, and this resulted in an empty bracket and no issues, so I think I know for sure that the form is being validated, and the POST request is working.
Code:
MODELS.PY
class Set(models.Model):
title = models.CharField(max_length = 64, null = False, blank = False)
description = models.CharField(max_length = 255, null = False, blank = True)
def __str__(self):
return self.title
class Entry(models.Model):
set = models.ForeignKey(Set, on_delete=models.CASCADE)
term = models.TextField()
definition = models.TextField()
def __str__(self):
return self.term
FORMS.PY
from django.forms import ModelForm
from django.forms.models import modelformset_factory
from .models import Set, Entry
class SetForm(ModelForm): # Form that maps to Set
class Meta:
model = Set
fields = ['title', 'description']
class EntryForm(ModelForm):
class Meta:
model = Entry
fields = ['set', 'term', 'definition']
VIEWS.PY
def editEntry(request, set_id):
EntryFormSet = modelformset_factory(Entry, EntryForm, extra=0)
set_obj=Set.objects.get(id=set_id)
entry_list = set_obj.entry_set.order_by("term")
entry_formset=EntryFormSet(queryset=entry_list)
if request.method == 'POST':
instances=entry_formset.save()
for instance in instances:
instance.save()
entry_formset = EntryFormSet(queryset=instances)
else:
entry_formset = EntryFormSet(queryset=entry_list)#formset_factory(entry_form)
return render (request, 'dictTemplates/editEntries.html', {'entry_formset': entry_formset})
EDIT ENTRIES.HTML
<h1 style="text-align:center"><strong></center>Edit Entries Page</strong></h1>
<form method="POST" action = "">
{% csrf_token %}
{{ entry_formset.management_form }}
<center><table id="entriesFormSet" class="table">
<input type ="submit" value ="Submit Form">
<tr>
<th><h3>Terms</h3></th>
<th><h3>Definitions</h3></th>
</tr>
<tbody>
{% for form in entry_formset %}
<tr>
<td>{{ form.term }}</td>
<td>{{ form.definition }}</td>
<td class="delete-entry-button"><input type = "submit" value = "Delete Term"></td>
</tr>
{% endfor %}
</tbody>
</table></center>
</form>
URLS.PY
urlpatterns = [
path('', views.home, name='Home'),
path('about/', views.about, name='About'),
path('sets/', views.sets, name='Sets'),
path('sets/create/', views.createSet, name='createSet'),
path('sets/edit/(?P<set_id>[\d]+)', views.editSet, name='editSet'),
path('sets/delete/(?P<set_id>[\d]+)', views.deleteSet, name='deleteSet'),
path('sets/view/(?P<set_id>[\d]+)', views.viewSet, name='viewSet'),
path('entrys/create/(?P<set_id>[\d]+)', views.createEntry, name='createEntry'),
path('entrys/edit/(?P<set_id>[\d]+)', views.editEntry, name='editEntry'),
path('entrys/delete/(?P<entry_id>[\d]+)', views.deleteEntry, name='deleteEntry'),
]
The desired result is that clicking "Submit" results in an updated form plus changes in the database, but right now, clicking "Submit" only results in an updated form at that moment - clicking to another page or any other action makes the form revert to the original state.
I think the problem is that my Submit button somehow isn't "mapping" to saving the form into the database, that there's a missing connection there somewhere, but I'm not sure how to find it.
Please let me know if I should format/post this question differently, as it's my first SO question. Thank you so much!! Any help would be very much appreciated!
Not sure if something else is also wrong, but your view doesn't instantiate your formset well. Try with the following code:
EntryFormSet = modelformset_factory(Entry, EntryForm, extra=0)
if request.method == 'POST':
entry_formset=EntryFormSet(data=request.POST)
if entry_formset.is_valid():
entry_formset.save() # this will save all the instances
else:
set_obj=Set.objects.get(id=set_id)
entry_list = set_obj.entry_set.order_by("term")
entry_formset=EntryFormSet(queryset=entry_list)
return render(request, 'dictTemplates/editEntries.html', {'entry_formset': entry_formset})
If you want you may assign return value to formset save like instances = entry_formset.save() and debug which instances are saved (that returned value will be a list of those that are saved to database) by passing it to template together with formset in context and show the value in template.

How to remove session variable in a template after it's job is done in django

I have a app called dashboard which is where I redirect all logged in users with an option to add articles by the user.
After the user hits Submit button in the form, the data is sent to /dashboard/article/save URL via POST and after the data is stored, the view returns HttpResponseRedirect to show_dashboard which renders dashboard.html with a session variable result.
In the dashboard template file, I have added a notify.js code to show acknowledgements to user. The problem is if this session var is defined, everytime the dashboard page is showed, the notification is triggered EVEN if the user didn't add an article.
(I'm new to using web frameworks so I do not know how this all works properly)
Some code:
dashboard/models.py:
class Article(models.Model):
id = models.IntegerField(primary_key=True)
ar_title = models.CharField(max_length=25)
ar_data = models.CharField(max_length=500)
user = models.ForeignKey(User,on_delete=models.CASCADE)
def getArticleTitle(self):
return self.title
def getArticleData(self):
return self.title
def getArticleAuthor(self):
return self.user
dashboard/urls.py:
urlpatterns = [
url(r'^$', views.show_dashboard,name='home_dashboard'),
url(r'^profile/save/', views.save_profile,name="save_profile"),
url(r'^newsfeed/', views.get_newsfeed,name="newsfeed",),
url(r'^profile/', views.show_profile,name="show_profile"),
url(r'^article/save/', views.add_new_article,name="add_new_article"),
]
dashboard/views.py:
#login_required
def show_dashboard(request):
return render(request,'dashboard/dashboard.html',{'form':NewArticleForm()})
def add_new_article(request):
if(request.method == 'POST'):
ar_title= request.POST['ar_title']
ar_data = request.POST['ar_data']
user = request.user
form = NewArticleForm(request.POST)
if(form.is_valid()):
Article.objects.create(ar_title=ar_title,ar_data=ar_data,user=user)
request.session["result"] = "add_article_OK"
return HttpResponseRedirect(reverse('home_dashboard'))
dashboard.html:
{% ifequal request.session.result 'add_article_OK' %}
<script>
$.notify("New article added successfully",
{position:"bottom right","className":"success","autoHide":"yes","autoHideDelay":"3000"});
</script>
{% endifequal %}
Now, how do I remove this session value after it has displayed the message? I know del request.session['result'] can be issued but where can I put it in this heirarchy of moves?
Do it in the show_dashboard view.
Instead of getting the value from the session in the template, pop it in the view and pass it to the template; that way you take care of getting and clearing it in one go.
#login_required
def show_dashboard(request):
context = {
'form': NewArticleForm(),
'result': request.session.pop('result', None)
}
return render(request,'dashboard/dashboard.html',context)
...
{% ifequal result 'add_article_OK' %}

getting html form data into django class based view

I have created a Class view in views.py of the django application.
class HelloTemplate(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(HelloTemplate, self).get_context_data(**kwargs)
return context
Now I have a form defined in the html page:
<form method="get">
<input type="text" name="q">
<input type="text" name="q1">
<input type="submit" value="Search">
</form>
As you can see, I am submitting the form on the same page.
Now I want to get the form submitted values in my HelloTemplate class. I don't want to create another class or methods outside the existing class.
Also, I would like to send an error message to the html form if data is not validated in the django.
I don't know how to do this, please help me out.
You need to define get (because your form defined with get method <form method="get">) method in view class:
class HelloTemplate(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(HelloTemplate, self).get_context_data(**kwargs)
return context
def get(self, request, *args, **kwargs):
q = request.GET.get('q')
error = ''
if not q:
error = "error message"
return render(request, self.template_name, {'error': error})
More information in django docs here Introduction to Class-based views
There's only one value, and it's in request.GET['q'].
Quick response, I can show you what I did a while ago for a review form (for people to create a new review, one of my models):
def review_form_view(request):
c = {}
c.update(csrf(request))
a = Review()
if request.method == 'POST':
review_form = Review_Form(request.POST, instance=a)
if review_form.is_valid():
a = review_form.save()
return HttpResponseRedirect('../dest_form_complete')
pass
else:
review_form = Review_Form(instance=a)
return render_to_response('../review_form.html', {
'review_form': review_form,
}, context_instance=RequestContext(request))
If you have a user model, comment model, etc. you can probably use something similar to this. Very (very) roughly put, the request is the input that the user fills out in the form, 'POST' is the method called that lets the server know you are adding entries to your database, and is_valid() validates the data according to your models.py parameters (can name be NULL? Is age an integer? etc).
Take a look at https://docs.djangoproject.com/en/dev/topics/forms/ as well for more examples and explanation.

Custom Django Form Not Saving

I am trying to use customize the output of my form by this method. For example:
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="id_message">Your message:</label>
{{ form.message }}
</div>
If I render the form as form.as_p, everything works fine, but rendering the fields individually does not work.
I have some fields that I want to be hidden so am trying to render each individually. The fields I am trying to hide are given null=True, blank=True attributes in the model class, and are therefore not required.
No errors are being shown, rather the page is refreshed and the data is not updated. I'm not sure what I could be missing given these factors. Here is the view:
The view is from userena:
#secure_required
#permission_required_or_403('change_profile', (get_profile_model(), 'user__username', 'username'))
def profile_edit(request, username, edit_profile_form=EditProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None, **kwargs):
"""
Edit profile.
Edits a profile selected by the supplied username. First checks
permissions if the user is allowed to edit this profile, if denied will
show a 404. When the profile is successfully edited will redirect to
``success_url``.
:param username:
Username of the user which profile should be edited.
:param edit_profile_form:
Form that is used to edit the profile. The :func:`EditProfileForm.save`
method of this form will be called when the form
:func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm`
from userena.
:param template_name:
String of the template that is used to render this view. Defaults to
``userena/edit_profile_form.html``.
:param success_url:
Named URL which will be passed on to a django ``reverse`` function after
the form is successfully saved. Defaults to the ``userena_detail`` url.
:param extra_context:
Dictionary containing variables that are passed on to the
``template_name`` template. ``form`` key will always be the form used
to edit the profile, and the ``profile`` key is always the edited
profile.
**Context**
``form``
Form that is used to alter the profile.
``profile``
Instance of the ``Profile`` that is edited.
"""
user = get_object_or_404(get_user_model(),
username__iexact=username)
profile = user.get_profile()
user_initial = {'first_name': user.first_name,
'last_name': user.last_name}
form = edit_profile_form(instance=profile, initial=user_initial)
if request.method == 'POST':
form = edit_profile_form(request.POST, request.FILES, instance=profile,
initial=user_initial)
if form.is_valid():
profile = form.save()
if userena_settings.USERENA_USE_MESSAGES:
messages.success(request, _('Your profile has been updated.'),
fail_silently=True)
if success_url:
# Send a signal that the profile has changed
userena_signals.profile_change.send(sender=None,
user=user)
redirect_to = success_url
else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username})
return redirect(redirect_to)
if not extra_context: extra_context = dict()
extra_context['form'] = form
extra_context['profile'] = profile
return ExtraContextTemplateView.as_view(template_name=template_name,
extra_context=extra_context)(request)
I am including the html to render the forms using {% include 'my-template.html' %}. What could be preventing me from updating the profile object? Thanks for any ideas!
Try adding
{{ form.errors }}
somewhere to your template to see if there are any non-field errors !

Categories