Django: If user has entered address previously - django

I want to present a user with a form if he has not filled it out previously on login but redirect him to the homepage if he has filled in information previously.
How do i accomplish this?
Here's my view:
def makinginfo(request):
form = SongForm(request.POST or None)
songprofile = SongProfile.objects.get().filter(user=request.user)
if songprofile = null: IS THIS RIGHT?
if form.is_valid():
form.save()
sp = SongProfile
sp.song = form.pk
sp.save()
if 'next' in request.POST:
next = request.POST['next']
else:
next = reverse('index_show')
return HttpResponseRedirect(next)
return render_to_response(
'song/create.html',
{'form':form},
context_instance = RequestContext(request)
)
else:
return render_to_response(
'song/show.html',
context_instance = RequestContext(request)
)
Am i on the right track here?
Thanks,
ADDITIONAL INFO:
SongProfile and Song are two different models. SongProfile model is as follows:
class SongProfile(models.Model):
song = models.OneToOneField(Song)
so when im trying to save both in song and songprofile where songprofile.song saves the latest id/pk of the record created in song. is this wrong?

I'm assuming there is only one SongProfile object per user.
try:
songprofile = SongProfile.objects.get(user=request.user)
# Render song/show.html
except SongProfile.DoesNotExist:
if form.is_valid():
# Process post
# Render song/create.html
To create a new SongProfile object with the Song created by the form:
song = form.save()
songprofile = SongProfile(user=request.user)
songprofile.song = song
songprofile.save()
EDIT AGAIN:
Fixed backwards stuff and added Song object.

You can, indeed, do a try/catch as Tim demonstrated and that will work. If you find that you are, in some cases, beginning to filter more fields or that you want a sensible default, you can do as the docs suggest and use the get_or_create() method, like so:
sp, created = SongProfile.objects.get_or_create(user=request.user)
From the docs:
Any keyword arguments passed to get_or_create() -- except an optional one called defaults -- will be used in a get() call. If an object is found, get_or_create() returns a tuple of that object and False. If an object is not found, get_or_create() will instantiate and save a new object, returning a tuple of the new object and True.
So the Boolean value indicates if the object needed to be created or not and you can use it to guide method logic.
This syntax, too, is a little briefer and cleaner.
As far as trying to save both Song and SongProfile, no, you're not wrong. As Tim demonstrated, form.save() creates the new song and songprofile.song = song, followed by the songprofile.save(), saves the songprofile's song reference.
There are a couple of small errors in the code example. One error is,
if songprofile = null:
...
which, were you to use it, as #Natim noted, should be,
if songprofile is None:
...
Another problem is in the line,
sp = SongProfile
which Python will, indeed, compile, but which assigns the SongProfile class object reference to the variable and not an instance of the class itself. What you want to do normally would be,
sp2 = SongProfile()
and that will create the object instance. If you do a dir(sp) and dir(sp2), you'll see the difference.

Related

Is there a method to create a persistent variable in my Django view function?

I'm currently grabbing a variable 'my_var' via GET when my view function is called, as shown. I need to query by this variable in my POST method.
def article_delete_view(request, pk):
my_var = request.GET.get('my_var') #GET THE VARIABLE from referring data-url
obj = Listing.objects.get(type=type) # I could query here if I wanted (illustration only)
article = get_object_or_404(Article, pk=pk)
data = dict()
if request.method == 'POST':
article.delete()
data['form_is_valid'] = True
articles = Article.objects.all()
obj = Listing.objects.get(name=my_var) #THIS DOES NOT WORK, since 'type' is not passed on submit.
context = {'articles':articles, 'obj':obj}
data['article_table'] = render_to_string('article_table.html', context)
else:
context = {'article':article}
data['html_form'] = render_to_string('article_delete.html', context, request=request)
return JsonResponse(data)
Is there a best-practice way to make that variable persist when POST is called on a submit? I know declaring a global is a bad idea. Thought about writing it to memory (attached to article) but that feels like a hack. Appending it to the pk argument then splitting it out feels even worse. Yuck. I could get my <input> method to pass my_var but to do so I'd have to refactor a lot of other things to make it happen. Are django session variables the answer here? Any perspective is helpful. Thanks in advance.
UPDATE & SOLUTION:
So to contextualize, the issue here is that I want to query a specific model instance within a view that does not have that model instance info (pk or id) passed to it as an argument. I can GET that variable through the data-url that fires the view, but only once. It was not usable in POST, or on any subsequent call to article_delete_view.
Imagine you're trying to make a peanut butter sandwich, but you can only retrieve one item at a time from the cupboard. You get the peanut butter on the first visit to the cupboard, but when you open the cupboard again to get the bread, the peanut butter disappears. Session variables turned out to be a solution for saving the peanut butter, you know, like a little shelf. A rolling wheelie shelf. Anyway:
def article_delete_view(request, pk):
my_var = request.GET.get('my_var')
if my_var is not None: #This prevents my_var from being lost subsequent function calls
request.session['session_var'] = my_var = request.GET.get('my_var') # create session variable
else:
pass
article = get_object_or_404(Article, pk=pk)
data = dict()
if request.method == 'POST':
article.delete()
data['form_is_valid'] = True
articles = Article.objects.all()
session_var = request.session.get('session_var', None) # grab the session variable here
obj = Listing.objects.get(name=session_var) # NOW THIS WORKS!
context = {'articles':articles, 'obj':obj}
data['article_table'] = render_to_string('article_table.html', context)
else:
context = {'article':article}
data['html_form'] = render_to_string('article_delete.html', context, request=request)
return JsonResponse(data)
The if that follows getting my_var prevents the session variable from being written as null on each subsequent call to the view, since GET only gets the variable the first time. If anyone is curious where that session_var is actually coming from, here's the source (obj.slug):
<button type="button" data-url="{% url 'article_delete' article.id %}?my_var={{obj.slug}}">Name</button>
I'm using GET to grab it from a template where it's in scope, so I can query with it in article_delete_view. Passing it in as a URL parameter did not work, since that pattern doesn't match anything in my app. So far this is working great. If anybody has any other ideas and/or can tell me why this is a bad idea, please post.

Django object "lock" and context rendering

I have a simple(I think) question, about Django context rendering.
I'll step right into it -
Basically what I need is, some temp table, which in my case, I called Locked. And when a user presses a button, which Is a form, that object goes straight to the table Locked(just a simple insert). And inside that table there is a field called is_locked, and if its True, that object needs to go gray, or to have some lock icon inside the html table.
Just some kind of a viewable sign, that an object is inside the table Locked, and that another user can't access it.
But, my problem is, since in my views.py, my lock function is not returning exact html where I want to render that locker icon, instead, it returns another html.
Is there any way, to render same context, on 2 html pages? Thank's.
This is my code :
views.py
def lock(request, pk):
# Linking by pk.
opp = get_object_or_404(OpportunityList, pk=pk)
opp_locked = get_object_or_404(Locked, pk=pk)
# Taking two parametters for 2 fields.
eluid = Elementiur.objects.get(eluid=pk)
user = User.objects.get(username=request.user)
# Dont bother with this one! Just pulling one field value.
field_name = 'optika_korisnik'
obj = OpportunityList.objects.get(pk=pk)
field_object = OpportunityList._meta.get_field(field_name)
field_value = getattr(obj, field_object.attname)
# This is the main part! This is where i'm inserting data into Locked table.
if opp_locked.DoesNotExist:
opp_locked.id = int(eluid.eluid)
opp_locked.locked_eluid = eluid
opp_locked.locked_comment = field_value
opp_locked.locked_user = user
opp_locked.locked_name = 'Zaključao korisnik - ' + request.user.username
opp_locked.is_locked = True
opp_locked.save()
# This is what has to be returned, but i need context on the other page.
return render(request, 'opportunity/detalji/poziv.html',
{'opp': opp, 'locked': opp_locked})
else:
# This return has the context that i need(from the first opp_locked variable)
return render(request, 'opportunity/opp_optika.html', {'locked_test': opp_locked})
I can provide more code, but i think that it's not important for this type of question, because all of the logic is happening inside the lock finction, and last two returns.
I just had a quick overview of your snippet sorry if this not help you but you need to review it a little bit.
You call DoesNotExist on an instance of a Locked model
if opp_locked.DoesNotExist: [...]
that's not how you should use this exception.
You have a method .exists() that is available but only for Querysets.
Also if your instance does not exists you are alredy returning an Http404 response when you use get_object_or_404() method.
And perhaps you should avoid sharing primary keys between instances and replace them with models.OneToOneField (OneToOnefield)
Since i got no answers, i added a new field, is_locked, into my Locked model and that solved it.

Set default value for dynamic choice field

I have a form that asks the user to enter in their zip code. Once they do it sends them to another form where there is a field called 'pickup_date'. This gets the value of the zip from the previous field and gets all of the available pickup_dates that match that zip code into a ChoiceField. I set all of this within the init of the model form.
def __init__(self,*args,**kwargs):
super(ExternalDonateForm,self).__init__(*args,**kwargs)
if kwargs:
zip = kwargs['initial']['zip']
self.fields['pickup_date'] = forms.ChoiceField(choices = self.get_dates(zip))
elif self.errors:
zip = self.data['zip']
self.fields['pickup_date'] = forms.ChoiceField(choices = self.get_dates(zip))
The problem I have is when there are other errors on the form. I use the elif self.errors to regenerate the possible choices but it doesn't default to the original selected option. It goes back and defaults to the first choice. How can I make it so it's default option on form errors is what was originally posted?
Change self.fields['pickup_date'] to self.fields['pickup_date'].initial and see if that helps.
I got it to work after playing around for a while. Above, I was setting all the dynamic choices with a get_dates() function that returned a tuple. Instead of doing that I returned a field object like this using a customized ModelChoiceField instead of a regular ChoiceField....
class MyModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return obj.date.strftime('%a %b %d, %Y')
Dates function
def get_dates(self,zip):
routes = Route.objects.filter(zip=zip).values_list('route',flat=True)
pickups = self.MyModelChoiceField(queryset = PickupSchedule.objects.filter(
current_count__lt=F('specials'),
route__in=routes,
).order_by('date')
)
if not pickups:
pickups = (('----','No Pickups Available At This Time'),)
return pickups
in the init i set the value for self.fields['pickup_date'] like so..
self.fields['pickup_date'] = self.get_dates(zip)

Need clarification on using Django 1.4 Form Wizards, specifically pre-filling and saving

We are building a wizard using Django 1.4's new form wizard functionality.
The docs on this are very terse and we can't find any advanced examples. We are using a named step wizard (needed to support a listview/datagrid we use) and a session backend.
The wizard is meant to edit roles and linked rights and is built to provide both add and edit functionality. We do this by asking the user in the first step if he/she wants to add or edit.
The next step depends on that choice;
If the user wants to edit, there is a search screen, followed by a listview/datagrid that displays results. The user can then select one of the results and goes to a details-screen, followed by a FilteredSelectMultiple page, allowing him/her to link rights to this role.
If the user wants to add a new role, the search and results screens are skipped and the user goes directly to the details screen, followed by the link-screen.
It all works pretty well, using a condition_dict in urls.py, but we are wondering a couple of things about the general functionality:
When a specific pre-existing role is selected, how can we fill the details and the link-screen with the corresponding data?
Do we instantiate a roles-object and pass it somehow to the two forms, if so, where do we instantiate it and do we need to do that for every form separately (which seems a bit over the top)?
When saving, is it common practice to create another instance of a role object, add the form data to it and save, or can we re-use the object used in the forms somehow?
We have tried overloading get_form_instance to return instances of roles, and we have looked at instance_dict in the docs, but it feels like the wrong approach and there are no examples to be found online, and we're not even sure these are used to pre-fill data or even if we're on the right track.
Logically, I would say in the step that selects an existing role, I need to fill the wizard-variables using an instance of the chosen object, and these get displayed in the forms. At the end of the wizard we reverse the process and get all data from the wizard-variables and add them to a newly instantiated roles-object and save it. Ideally this instance will determine itself if it needs to perform an INSERT or an UPDATE, depending on whether or not the promary key is filled.
If anyone can provide an example, or a nudge in the right direction, it would be very much appreciated.
The code of the wizardview class in views.py is below:
class RolesWizard(NamedUrlSessionWizardView):
def get_template_names(self):
# get template for each step...
if self.steps.current == 'choice':
return 'clubassistant/wizard_neworeditrole.html'
if self.steps.current == 'search':
return 'clubassistant/wizard_searchrole.html'
if self.steps.current == 'results':
return 'clubassistant/wizard_pickrole.html'
if self.steps.current == 'details':
return 'clubassistant/wizard_detailsrole.html'
elif self.steps.current == 'rights':
return 'clubassistant/wizard_roles.html'
def get_context_data(self, form, **kwargs):
# get context data to be passed to the respective templates
context = super(RolesWizard, self).get_context_data(form=form, **kwargs)
# add the listview in the results screen
if self.steps.current == 'results':
# get search text from previous step
cleaned_data = self.get_cleaned_data_for_step('search')
table = RolesTable(Roles.objects.filter(
role_name__contains=cleaned_data['searchrole'])
)
RequestConfig(self.request, paginate={
"per_page": 4,
}).configure(table)
# add the listview with results
context.update({'table': table})
# add a role instance based on the chosen primary key
if self.steps.current == 'rights':
cleaned_data = self.get_cleaned_data_for_step('results')
role_id = cleaned_data['role_uuid']
role = get_object_or_404(Roles, pk=role_id)
context.update({'role': role})
return context
def done(self, form_list, **kwargs):
# this code is executed when the wizard needs to be completed
# combine all forms into a single dictionary
wizard = self.get_all_cleaned_data()
if wizard.get("neworeditrole")=="add":
role = Roles()
else:
role = get_object_or_404(Roles, pk=wizard.get("role_uuid"))
# many-to-many rights/roles
role.role_rights_new_style.clear()
for each_right in wizard.get('role_rights_new_style'):
RightsRoles.objects.create(role=role, right=each_right,)
# other properties
for field, value in self.get_cleaned_data_for_step('details'):
setattr(role, field, value)
role.save()
# return to first page of wizard...
return HttpResponseRedirect('/login/maintenance/roles/wizard/choice/')
For future googlers:
I had some success with using get_form() because it is called before a form is rendered. Start with a couple of ModelForms:
class Wizard1(models.ModelForm):
class Meta:
model = MyModel
fields = ('field0', 'model0')
class Wizard2(models.ModelForm):
class Meta:
model = MyModel
excludes = ('field0', 'model0')
Then, in your SessionWizardView:
class MyWizard(SessionWizardView):
def get_form(self, step=None, data=None, files=None):
form = super(ExtensionCreationWizard, self).get_form(step, data, files)
if step is not None and data is not None:
# get_form is called for validation by get_cleaned_data_for_step()
return form
if step == "0":
# you can set initial values or tweak fields here
elif step == "1":
data = self.get_cleaned_data_for_step('0')
if data is not None:
form.fields['field1'].initial = data.get('field0')
form.fields['field2'].widget.attrs['readonly'] = True
form.fields['field3'].widget.attrs['disabled'] = True
form.fields['model1'].queryset = Model1.objects.filter(name="foo")
return form
The action is all in step 1. You request validated data from step 0 (which triggers another call to get_form() for step 0, so be careful) and then you can access any values that were set in step 0.
I threw in a couple of examples of settings you can change on the fields. You can update a queryset to limit the values in a ChoiceField, or re-display a value again but make it read-only. One caveat I noticed... readonly does not work on ChoiceField. You can make it disabled, but then the value is not propagated when you submit the form.
Let's see if I can help. I did a form wizard that adds steps depending on the answers. At each step I save all forms in a session variable, like so:
def process_step(self, request, form, step):
request.session['form_list'] = self.form_list
request.session['initial'] = self.initial
Then, each time that view is rendered, I instantiate a new form wizard with all the previous data:
def dynamic_wizard(request):
if not request.session.get('form_list'):
form = Wizard([Form1])
else:
form = Wizard(request.session.get('form_list'), initial = request.session['initial'])
return form(context=RequestContext(request), request=request)

Best way to handle request variables in Django

I have a form 'in the wild' that takes many different variables - which may or may not be populated.
try:
app_version = request.REQUEST["appVersion"]
except:
app_version = ''
try:
app_name = request.REQUEST["appName"]
except:
app_name = ''
try:
app_code_name = request.REQUEST["appCodeName"]
except:
app_code_name = ''
Is there a tighter way to accomplish this?
app_version = request.REQUEST.get("appVersion", "")
get(key, default) is a method implemented on Python dicts. If the key exists in the dictionary, its value is returned; if the key does not exist, the specified default value is returned. In Django, request objects are dictionary-like objects, so get is also defined for them in the same manner.
If these variables are intended to populate a form, then you can safely pass the request.POST object directly into the form constructor.
if request.method == 'POST':
form = MyForm(request.POST)
The form will automatically pass the correct values to the correct form fields and use defaults for keys that don't exist and will still create blank fields for missing keys (see addendum).
If you are trying to process a form, it is still better to create a form object as above, and read out the values from that object.
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# You may process these variables here
print form.appVersion
print form.appName
print form.appCodeName
Remember, validation code is best placed in the form class as well. That way, if form.is_valid() returns True, then you know you have a clean dataset to work with.
Note: Django docs recommend using request.POST or request.GET directly rather than the amalgamated variable request.REQUEST, as it is more explicit.
Addendum:
It is important to understand the difference between bound and unbound forms in this case. If you create an unbound form with form = MyForm(), then when the form is instantiated, it will fill in all fields with the initial property of each field (if it exists). For example, with this code:
from django import forms
class MyForm(forms.Form):
appVersion = forms.CharField(initial='1.0')
appName = forms.CharField()
appCodeName = forms.CharField()
the form will be initialized with appVersion having a value of '1.0'. However, if you bind a POST request to a form like this: form = MyForm(request.POST), then the initial properties are ignored. That means if the POST dict does not include an appVersion key, then that field will be left blank. As long as the field is not required, your form will still validate, and you can modify form.appVersion in the view after validation.
If you have many fields, a more compact version might be:
defaults = { 'field1' : 'val1', 'field2' : 'val2', ...}
defaults.update(request.POST)