I'm really confused how I can change the registration_form to ask the users about their first and last name.
I think I need to manipulate subclass RegistrationView from registration.backends.simple.views but I don't know how to do this to show first and last name in registration form.
You should be able to sub-class RegistrationForm and add the fields you want.
Look at the source for the default forms on how to do so:
https://github.com/macropin/django-registration/blob/master/registration/forms.py
Related
Is it possible to create and delete new charfields or textareas through the Django admin page without harcoding them?
For example, I have a simple model, registered in Django admin page
class DocumentList(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
Obviously, it has only one charfield on admin page, something like:
DocumentList: [___________]
How can I add another one and delete her later if needed from Django admin page without actually hardcoding another charfield/textarea in models.py, to make it look like:
DocumentList: [___________]
*****************[___________]
Django models are not meant to be dynamically altered. You have to explicitly add the fields on your model, run migrations to have the fields created in your database backend, and reload your server process (./manage.py runserver does this automatically).
If you want to create a model that can hold an arbitrary amount of text strings instead of just one or a fixed amount, you need to use a many-to-many relation to another model.
You can use a custom form in the admin, either by using the form option of the get_form method. This is the documentation example for how you'd pass a custom form:
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm
You can add extra fields, as in any form.
I was wondering why you wanted this. Since you said in a comment it is to submit information to an API, you can also use an action, taking input from the user in an intermediate page.
EDIT: As became apparent in comments, the form needs to be dynamic for the user, and not when it is created. Therefore, the solution is using inlines, which once created and linked to the current model, allow the user to add any number of related forms to the current form.
I am trying to add a non-model form in django admin interface and am not able to find any particular way to do it. This form would do some processing and change some data in the DB. But this is not related to a particular Model and should stand out. This form should not be available for the user to use.
One thing I can do is add the form to the general view and prohibit using permissions but I was thinking since django admin interface already exists, it would be better to add that to the django admin interface.
Is this possible to do in Django?
You can add arbitrary views that within a ModelAdmin that do whatever you want. See the documentation for ModelAdmin.get_urls. You can do the same at a higher level by defining AdminSite.get_urls.
I was building a login/logout User system with django, and I started playing around with the django auth system.
I am using the custom Django UserCreationForm like so:
views.py
from django.contrib.auth.forms import UserCreationForm
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect("/books/")
else:
form = UserCreationForm()
return render(request, "registration/register.html", {
'form': form,
})
But this form renders a lot of unwanted stuff, as shown here: https://app.box.com/s/wmrtyal3mctb9hctsnom
First, I would like to get rid of the added information, is there a good way to do this? Are there any good ways to do this? Is there a way to edit the UserCreationForm myself or would I have to create my own form?
Second, lets assume I want to add more required fields to a registration page, what is the best way to do this? Say I wanted to make it the case that a person register male or female -I realize I should extend the user model, but how would I register both to the original user model and the extended user model?
Rather than extend the UserCreationForm class, you should rewrite it. Check out the source for it here.
For example, if you wanted to get rid of the text next to the password field that says 'Enter the same password as above', remove this line:
help_text=_("Enter the same password as above, for verification."))
from django /django/contrib/auth/forms.py (in the link above)
EDIT: The reason I suggested that you rewrite the forms classes rather than extending them is because it is simply more convenient this way. From the Django docs:
If you don’t want to use the built-in views, but want the convenience
of not having to write forms for this functionality, the
authentication system provides several built-in forms located in
django.contrib.auth.forms
If you were to extend your User model classes you would need to change the form classes accordingly. Again from the docs:
As you may expect, built-in Django’s forms and views make certain
assumptions about the user model that they are working with. If your
user model doesn’t follow the same assumptions, it may be necessary to
define a replacement form, and pass that form in as part of the
configuration of the auth views.
See here and here for more details
If you simply want to change the UserCreationForm you would edit the UserCreationForm class as in the forms.py module as I mentioned above.
If, as you mentioned in your question, you want to add more required fields to a registration page, you would need to make a custom User model. See here for details on how to do this
I want to override the index view present in class AdminSite in django.contrib.admin.sites. What I want to do through this is I want to check here if user is superuser then show index.html template with all models otherwise if user is an normal staff user then show him/her a test.html template with different content.
The auth package lets you set up permissions on a model-by-model basis out of the box. There's not necessarily a need to override the view. See https://docs.djangoproject.com/en/dev/topics/auth/#permissions
I know that you can prepopulate admin form fields based on other fields. For example, I have a slug field that is automatically populated based on the title field.
However, I would also like to make other automatic prepopulations based on the date. For example, I have an URL field, and I want it to automatically be set to http://example.com/20090209.mp3 where 20090209 is YYYYMMDD.
I would also like to have a text field that automatically starts with something like "Hello my name is author" where author is the current user's name. Of course, I also want the person to be able to edit the field. The point is to just make it so the user can fill out the admin form more easily, and not just to have fields that are completely automatic.
I know that you can prepopulate some values via GET, it will be something like this
http://localhost:8000/admin/app/model/add/?model_field=hello
I got some problems with date fields but, maybe this could help you.
I recently used Django's ModelAdmin.get_form method for this purpose.
class MyModelAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['my_field_name'].initial = 'abcd'
return form
Yout should be careful about side effects as you are manipulating the base_fields directly.
Django's built-in prepopulated_fields functionality is hardcoded to slugify, it can't really be used for more general purposes.
You'll need to write your own Javascript function to do the prepopulating. The best way to get it included in the admin page is to include it in the inner Media class of a custom Form or Widget. You'll then need to customize your ModelAdmin subclass to use the custom form or widget. Last, you'll need to render some inline Javascript along with each prepopulated field to register the onchange handler and tell it which other field to populate from; I would render this via the custom Widget. To make it nice and declarative you could use a custom ModelAdmin attribute (similar to prepopulated_fields), and override ModelAdmin.formfield_for_dbfield to create the widget and pass in the information about what field it should prepopulate from.
This kind of admin hacking is almost always possible, but (as you can tell from this convoluted summary) rarely simple, especially if you're making an effort to keep your code nicely encapsulated.
I tried a few of these answers and none of them worked. I simply wanted to prepulate a field with another field from a related model. Taking this answer as a starting point, I finally tried to manipulate the model instance object (here obj) directly and it worked for me.
class MyModelAdmin(models.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(MyModelAdmin, self).get_form(request, obj, **kwargs)
if not obj.some_model_field:
obj.some_model_field = obj.related_model.prepopulating_model_field
return form
You can override the default django admin field by replacing it with a form field of your choice.
Check this :
Add custom validation to the admin
I would also like to have a text field
that automatically starts with
something like "Hello my name is
author".
Check out the docs at: http://docs.djangoproject.com/en/dev/ref/models/fields/#default
You could have a CharField() or TextField() in your model, and set this option, which will set the default text. 'default' can also be a callable function.
Something like:
models.CharField(max_length=250, default="Default Text")
The slug handling is done with javascript.
So you have to override the templates in the admin and then populate the fields with javascript. The date thing should be trivial, but I dont know how you should get the logged in users name to the script (not that I have thought very hard but you get the drift :).