django-views

How to create password input field in django

怎甘沉沦 提交于 2019-11-30 02:37:45
Hi I am using the django model class with some field and a password field. Instead of displaying regular plain text I want to display password input. I created a model class like this: class UserForm(ModelForm): class Meta: password = forms.CharField(widget=forms.PasswordInput) model = User widgets = { 'password': forms.PasswordInput(), } But i am getting the following error: NameError: name 'forms' is not defined. I am using django version 1.4.0. I followed this link : Django password problems Still getting the same error. What should i do. Where am i getting wrong.Please help DrTyrsa You

Populate a django form with data from database in view

本小妞迷上赌 提交于 2019-11-30 02:23:24
I have a form in my forms.py that looks like this: from django import forms class ItemList(forms.Form): item_list = forms.ChoiceField() I need to populate the item_list with some data from the database. When generated in HTML item_list should be something like: <select title="ItemList"> <option value="1">Select Item 1</option> <option value="2">Select Item 2</option> </select> The options values in my select statement will change almost every time since a variable in the query will often change generating new results. What do I need to put in the view.py and also in my template files to

Django formset set current user

我的梦境 提交于 2019-11-30 02:02:05
Related to this question , but expanding on it - How would I use this technique in a formset? I'd like to use the current logged in user in a form, but I'm using the form in a formset. The referenced solution for a single form is to pass request.user to the form and process in init. How do I add to the kwargs for each form in the formset? Example in my code: in forms.py class NewStudentForm (forms.Form): username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$', help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).

django - what goes into the form action parameter when view requires a parameter?

白昼怎懂夜的黑 提交于 2019-11-30 01:28:35
This is what I have: myview.py with a view that takes a parameter user : def myview(request, user): form = MyForm(request.POST) .... return render_to_response('template.html',locals(), context_instance=RequestContext(request)) The user gets passed through an url. urls.py : ... urlpatterns += patterns('myview.views', (r'^(?P<user>\w+)/', 'myview'), ) ... I also have a template.html with a form: <form name="form" method="post" action="."> ... </form> What goes in the the form action parameter if myview function requires a parameter? Right now I have action="." . The reason I'm asking is because

NameError: name 'request' is not defined, in Django forms

混江龙づ霸主 提交于 2019-11-29 23:57:10
问题 How do I get the current logged in user in forms.py ? I am trying to pre-populate the email field of the current user. class ContactMe(forms.Form): name = forms.CharField(label = "Name") email_address = forms.CharField(label = "Email Address", intital = request.user.email) subject = forms.CharField(label = "Subject") message = forms.CharField(label = "Message", widget=forms.Textarea(attrs={'cols': 10, 'rows': 3})) additional_comments = forms.CharField(required = False) class Meta: model =

Union and Intersect in Django

我怕爱的太早我们不能终老 提交于 2019-11-29 23:54:00
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple models just to ask my question. I wonder how can i query blogs using tags in two different ways. Blog entries that are tagged with "tag1" or "tag2": Blog.objects.filter(tags_in=[1,2]).distinct() Blog objects that are tagged with "tag1" and "tag2" : ? Blog objects that are tagged with exactly "tag1" and "tag2" and nothing else : ?? Tag and Blog is just used for an example. You could use Q objects for #1: # Blogs who have

404 Error for django serving static files. How to setup django to serve static files?

假装没事ソ 提交于 2019-11-29 23:31:38
问题 Settings.py file in Project directory : I already added following : STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ] -> in template <script src="{% static "validateurl/home.js" %}"></script> I have a directory static under project directory. home.js is under validateurl directory under static . Referring JS File using following in template html file : Please advise what am I missing here. Errors below : 回答1: Here are the steps to configure your django app to serve static files for

Delete multiple objects in django

送分小仙女□ 提交于 2019-11-29 20:47:57
I need to select several objects to be deleted from my database in django using a webpage. There is no category to select from so I can't delete from all of them like that. Do I have to implement my own delete form and process it in django or does django have a way to already do this? As its implemented in the admin interface. Matt Luongo You can delete any QuerySet you'd like. For example, to delete all blog posts with some Post model Post.objects.all().delete() and to delete any Post with a future publication date Post.objects.filter(pub_date__gt=datetime.now()).delete() You do, however,

Use get_queryset() method or set queryset variable?

眉间皱痕 提交于 2019-11-29 20:20:27
These two pieces of code are identical at the first blush: class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' queryset = Poll.active.order_by('-pub_date')[:5] and class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' def get_queryset(self): return Poll.active.order_by('-pub_date')[:5] Is there any difference between them? And if it is: What approach is better? Or when setting queryset variable is better than override the get_queryset method? And vice versa. In your example,

django - update model with FormView and ModelForm

夙愿已清 提交于 2019-11-29 19:38:53
I can't figure out how to use a ModelForm in a FormView so that it updates an already existing instance?? The form POSTs on this URL: r'/object/(?P<pk>)/' I use a ModelForm (and not directly an UpdateView ) because one of the fields is required and I perform a clean on it. I'd basically like to provide the kwarg instance=... when initializing the form in the FormView (at POST) so that it's bound to the object whose pk is given in the url. But I can't figure out where to do that... class SaveForm(ModelForm): somedata = forms.CharField(required=False) class Meta: model = SomeModel # with attr