I need to add custom fields to Django-registration-redux such as picture and few other fields. Is there a ref or 2 liner guide for how to do it? As per docs I can call RegistrationView in my app views and import my own created model fields to override it. If this is right approach please let me know.
You have to define your custom form and then set your registration url to use the custom form.
Defining your custom form:
users/form.py
from registration.forms import RegistrationForm
from users.models import User
class CustomRegistrationForm(RegistrationForm):
class Meta:
model = User
fields = ('email', 'picture', 'password',)
Your main urls file:
project_name/urls.py
from users.forms import CustomRegistrationForm
urlpatterns = [
[...]
url(r'^registration/register/$', RegistrationView.as_view(
form_class=CustomRegistrationForm), name='registration_register',),
url(r'^registration/', include('registration.backends.default.urls')),
[...]
]
Related
Any ideia how to implement form field with plus icon to add ? Like form in django admin.
Thank You
This can be done using ModelAdmin.autocomplete_fields. For example:
# admin.py
from django.contrib import admin
from .models import Car, Driver
#admin.register(Car)
class CarAdmin(admin.ModelAdmin):
autocomplete_fields = [
"driver",
]
#admin.register(Driver)
class DriverAdmin(admin.ModelAdmin):
search_fields = [
"name",
]
I tried to register the views in Django 2.2 admin interface, But I am not sure how to add it or can we able to add it.?
When I tried I am getting the below error.
admin.site.register(TargetView)
File "/usr/local/lib64/python3.6/site-packages/django/contrib/admin/sites.py", line 102, in register
for model in model_or_iterable:
TypeError: 'type' object is not iterable
from django.contrib import admin
from django.http import HttpResponse
from django.urls import path
#Create a Target model
class Target(models.Model):
class Meta:
verbose_name_plural = 'Targets'
app_label = 'apps' # provide app name
# View
def target_view(request):
return HttpResponse('Target View')
# Register
class TargetAdmin(admin.ModelAdmin):
model = Target
def get_urls(self):
view_name = '{}_{}_changelist'.format(
self.model._meta.app_label, self.model._meta.model_name)
return [
path('target/', target_view, name=view_name),
]
admin.site.register(Target, TargetAdmin)
No its not, a view (from the name) is responsible of what will be viewed to the user.
So it can not be controlled by you in the admin, and there is no need for that
But Models is data you might need in views which is why you can control in the admin, because sometimes you want to delete, edit or add objects.
I followed the Django documentation to try to extend my user model to add a field called authentication_key. It is resulting in an HTTP 400 error when I test the site locally.
My models:
class AuthenticationKey(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
authentication_key = models.CharField(max_length=100)
My Admin:
class AuthenticationKeyInline(admin.StackedInline):
model = AuthenticationKey
can_delete = False
verbose_name_plural = 'AuthenticationKey'
# Defines a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (AuthenticationKeyInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
And how I'm referring to the field in my views.py
#login_required(login_url='/login/')
def home_page(request):
u_id = request.user.authenticationkey
url = "http://example.com/api/allrequests/?format=json"
response = urlopen(url)
data = response.read().decode("utf-8")
j_data = json.loads(data)
j_data = json.dumps(j_data)
url = "http://example.com/api/past24hrdialoguebyid/" + str(u_id) + "/?format=json"
response = urlopen(url)
m_data = response.read().decode("utf-8")
return render(request,'index.html',{"u_id":u_id,"j_data":str(data),"m_data":str(m_data)});
I'm thinking the error may be how I'm referring to the field in views but I can't figure out quite what's wrong.
Here are my URL Patters:
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', home_page),
path('public/', public),
path('room-queue/', room_queue),
path('all-requests/', all_requests),
path('add-data/', add_data),
path('room-api/', room_api),
path('delete-data/', delete_data),
path('login/', auth_views.LoginView.as_view(template_name='admin/login.html')),
path('index-add-guest/', add_guest),
path('send-reply/', send_reply),
path('room-ready/', room_ready),
]
request.user.authenticationkey does not exist, since you have not defined authenticationkey as a property of the User model.
As already defined in your code, you should access the authentication key this way:
from .models import AuthenticationKey
authkey = AuthenticationKey.objects.get(user=request.user)
u_id = authkey.authentication_key
if you want to access authenticationkey as a property of user then you have to extend the User model by subclassing AbstractUser or AbstractBaseUser.
For instance, in your models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
authenticationkey = models.CharField(max_length=100)
In settings.py file, update the AUTH_USER_MODEL
AUTH_USER_MODEL = 'appname.models.User'
make sure you apply the migrations. You should then be able to access
request.user.authenticationkey
remember to update your admin too
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)
What i am trying to do:
Trying to registering my model to django admin
What problem do i get:
I am getting the following error:
My Code:
admin.py:
from django.contrib import admin
from .models import UserProfile, Post
admin.site.register(UserProfile,Post)
model.py:
from django.contrib.auth.models import User
from django.db import models
class UserProfile(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
avatar = models.ImageField(upload_to='static/media/',max_length=100)
class Post(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
urls.py:
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^register/$',views.registerUser),
url(r'^$', views.index, name="Index"),
url(r'^validateRegisterForm/$',views.validateRegisterForm),
url(r'^validateLoginForm/$',views.validateLoginForm),
url(r'^article/$', views.article, name="Article"),
url(r'^Login/$',views.loginUser, name="Login"),
url(r'^Logout/$',views.logoutUser, name="Logout"),
]
Note: I am new to django so, don't know much about it.
This should be
admin.site.register(UserProfile)
admin.site.register(Post)
Admin.site.register takes two parameters, the first is the model class and the optional second parameter is an admin class. In your code, you were passing another model as the admin class.
for more information, please refer to :https://docs.djangoproject.com/en/1.10/ref/contrib/admin/
I try to create a view, that will accept POST requests and create new instances of my model(see bottom of post). I follow this tutorial. The problem is that when I access URL associated with view, which inherits from CreateAPIView I dont see a form in html representation of API for creation of new instances and I also see that it accepts GET requests, not POST as it mentioned in documentation.
Page looks like this
My views.py
from django.shortcuts import render
from rest_framework.generics import ListAPIView, CreateAPIView
from datingapp.models import Profile
from .serializers import ProfileSerializer, ProfileCreateSerializer
class ProfilesAPIView(ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
class ProfileCreateAPIView(CreateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileCreateSerializer
My urls.py
from django.conf.urls import url
from django.contrib import admin
from datingapp.views import ProfilesAPIView, ProfileCreateAPIView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
]
My serializers.py
from rest_framework.serializers import ModelSerializer
from datingapp.models import Profile
class ProfileSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
In my settings.py I have crispy_forms installed.
What am I doing wrong ?
UPD: here is what I want to achieve
As you see there is a form and it accepts only POST and also says that GET is not allowed
The problem is in your router. The first pattern matches both api/profiles/ and api/profiles/create/ so the second one will never be evaluated. You are seeing the ProfilesAPIView instead of the create view.
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
To fix it, either swap the order of the urls, or add a $ to the end of the first pattern. r'api/profiles/$'
I was following a tutorial and had a similar problem. Probably I was not following the same version of Django Rest Framework and they had changes.
But I solved this problem doing this.
class AssetBundleList(generics.ListAPIView):
to
class AssetBundleList(generics.ListCreateAPIView):
Hope this helps someone.