Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to dynamically change the required attribute on form fields. The reason for doing this is because a user can select "Same address as previous user" yes/no.
If yes then it hides the fields on the frontend and I would want to make the fields which are required by default not required when validating / processing the modelform.
Here is a very nice discussion of this topic in general: Dynamic form requirements in Django .
If you just want to do something really simple, there are two very basic ways that I can think of:
Set the field to not be required and use a custom clean function to check that it exists when it should exist. (If you want an asterisk to appear after the field title, just use some simple javascript.)
Have two different forms--one with the field required and one without--and use javascript to display the correct form.
The first solution is obviously much simpler for exactly what you asked, but if you want to do something even slightly more complicated, you might prefer the second option.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Firstly, the scenario in my question:
I want to deliver a form in bitesize stages, with the valid completion of one stage leading to the next until all stages are complete.
On completion, I'd like to use data once, and then forget it completely so I'm not holding on to any user data at all.
My options as I see it are:
Multiple views each with a unique form all bound to a single model. The final submit button in the chain triggers data in the model to be accessed, used and then that particular row removed.
Multiple views each with unbound forms, each adding data to a cookie, which is then read on the final submit, data used and the cookie deleted (either using sessions or plain old js hard coded into each template).
A single view and template containing a single unbound form, which progressively un-hides divs containing each stage until all stages are completed and final 'submit' posts data from the form, allows a view to process it and forgets it.
I can actualise all three of the above, but which is the most 'Djangonic' method, or is there a better method still?
There is a library made from the Django guys for this case: https://django-formtools.readthedocs.io/en/latest/wizard.html
It's not dynamic though, so when you click "proceed", it will render a new page. But you can probably write a javascript module, that can handle this. But I wouldn't go that far, it's a lot of work for little benefit. Just just django-form-tools and use the wizard you want and do whatever you need in the done stage of your view.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
As of now i am facing some problem with field bind issue while get the response from coveo, inside the this method ToCoveoFieldName is implemented. Any one can help me on this.
Code snippet
raw.<%= ToCoveoFieldName("field Name", false)
Since you are having issues with this call, I am assuming that you might be migrating to the Coveo for Sitecore Hive Framework implementation, which mostly shifted from a back-end implementation to a front-end one for all of its major field logic.
This means that ToCoveoFieldName is not available any more on the server side. Instead, there is a JavaScript implementation of the same logic.
For instance, if you want to translate a field to the Coveo format, you can use CoveoForSitecore.Context.fields.toCoveo("field name").
I can see in your question that you are within a result template, there are already two helpers to get you either the field name or the value.
<div data-field={{= coveoFieldName("field name") }}>
and
<div>{{= coveoFieldValue("field name") }}</div>. (This one is the equivalent of calling raw[coveoFieldName("field name")])
Those helpers are explained in more details in the documentation, on the Editing the Content of a Result Template page (for version 5.0+) and Coveo Fields Resources Component page (for versions <5.0 and 4.1+).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to create a form that does the following: the user specifies the number of questions that they want to ask and then on the next page, the user is able to fill out the relevant details pertaining to the number of questions that they to ask. I was wondering if there is a simple way to do this using django forms?
My approach is as follows:
I use one form to record the number of questions that the user wants to ask with a general description, then when the user clicks submit/next, the next view would create a form with those number of questions; however, I'm running into a complication when trying to use this method. I want to link the first form to the second form, but not sure how to because I can't preserve any information in the view from each call.
Is there a better way of going about this problem?
You certainly can preserve information between views. Perhaps the easiest way to do this is in URL parameters; your first view can redirect to "/second_view/?number_of_questions=5" and the second view can access request.GET['number_of_questions'].
An alternative, which is particularly useful if you have a lot of data, would be to use the session; add the data to the session on submit in the first view, and pop it out in the second.
In Django 1.8, the django.contrib.formtools module was moved into a separate package, django-formtools. It includes the FormWizard, which is the standard tool you'd use to create a form that spans multiple pages and/or steps.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Question about DRF serializers. Especially ModelSerializer.
There are two versions with Foreign Key:
field = CharField(source='user.name')
field = SeriaizerMethodField()
get_field(self, obj):
return obj.user.name
What will work better / quickly ?
UPD
yes, I know about select/prefetch_related.
But is better just to pull required field from database and serialized it automaticaly or pull some 'raw' fields and nicely join them in serializer?
There are no difference between them. If you want to improve speed you should do it in a view by calling select_related method of a queryset.
YourModel.objects.select_related('user')
It'll join user table and calling user.name atribute will not hit a database next time
First one provides different validation options from the box. While the second one is something very customizable and doesn't provide any validation itself.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Using the Django framework, I've built a user-based web application. After some time, my client requested that the application disallow creating usernames that contain users' last names.
I have two questions:
Is it possible to do this reliably, always catching someone trying to register a username containing their last name, which is also present as a sign-up field? (My understanding and experience is that users can easily subvert this and still make the username look like their last name just by adding special characters or integers.)
What would be the best way to do this?
Try creating a custom validation rule. The function (which would be a member method of a subclass of django.forms.Field) might look something like this:
def validate(self, value):
if len(re.findall(self.lastname,value)) > 0:
raise forms.ValidationError('Your username may not contain your last name.')
return value
I don't think you can accomplish this withoud knowing their last name first. For example if I can make my username:
joevasquez OR jvasquez OR vasquezj OR vasquez...
There are no rules on what constitutes someome's last name. What if someone's last name is joe? It's not like filtering swearwords.
What is the reason behind this request? If it's privacy you can inform the user, but I don't think you can stop people from using their last names.
You can simply have your app autogenerate the usernames? Or use email as username?