I'm trying to get the django-profiles work.
I follow the steps of this manual ("The Missing Manual"),
so:
I work in the same project I also use for the django-registration (so no app created!!!)
In my templates I've created a folder "/profiles" (including edit_profile.html)
My AUTH_PROFILE_MODULE is set to 'myProjectName.UserProfile'
I've created in my project folder models.py containing:
from django.db import models
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(_('first name'), max_length=100)
middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
last_name = models.CharField(_('last name'), max_length=100)
birth_date = models.DateField(_('birth date'), blank=True, null=True)
In my project folder, I've created forms.py, with exactly the same code as in the manual
In urls.py of my project I've added:
from myProjectName.forms import ProfileForm
('^profiles/edit', 'profiles.views.edit_profile',{'form_class':ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
So now the whole urls.py file of my project is:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib import admin
from myProjectName.forms import ProfileForm
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', admin.site.urls),
(r'^accounts/', include('registration.urls')),
(r'^$', direct_to_template,
{ 'template': 'index.html' }, 'index'),
('^profiles/edit', 'profiles.views.edit_profile', {'form_class': ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
)
When I access now http://127.0.0.1:8000/profiles/edit/, I get this error:
Exception Value: No module named ourcrestmont.itaco.models
That's the import located in forms.py ...
What am I doing wrong?
EDIT:
The code of my forms.py:
from django.db import models
from django.forms import ModelForm
from ourcrestmont.itaco.models import *
class ProfileForm(ModelForm):
class Meta:
model = Foo
exclude = ('field1','field2','field3',)
It's exactly the same code as in the manual, or is this code not good? (could anyone give an other, better code?)
So that import statement appears to be attempting to import a file called "ourcrestmont/itaco/models.py". Make sure that exists.
(by django model, that looks like it is a project called ourcrestmont with an app called itaco)
If it exists, make sure that itaco and ourcrestmont both have init.py files in them.
You can just have the project (the import line in that case is from projectname.models import modelname) but I'd recommend sticking to the standard layout and putting your models, forms and views together in an App beneath a project, even if you only currently see your project as having one App. (Because it doesn't, it has at least three, since registration and profiles are also django apps, they're just django apps outside of what you've made, if you see what I mean, and thinking of it like that might make dependancy and inclusion bugs easier to solve. Certainly every other django app is going to assume most things are inside an app)
Related
I am using Django 2.2. I don't know what I'm missing.
models.py
from django.db import models
class Efesto(models.Model):
nombre = models.CharField(max_length=150)
tipo = models.ForeignKey(Color, blank=True, null=True, on_delete=models.CASCADE)
....
def __str__(self):
return self.nombre
admin.py
from django.contrib import admin
from estrategia import models
# Register your models here.
admin.register(models.Efesto)
Do I need anything else?
When I open the admin, I can't see the Efesto model there. The admin.py file is created automatically by the startapp command. The urls include
...
path('admin/', admin.site.urls),
It has being a while since I code django, and this used to be enough to get the models registered. The app is included in settings.INSTALLED_APPS correctly. Any advice will help.
You have to use admin.site.register(models.ModelName) in order to show the model in django admin.
You can find more about this in their official documentation
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/
I'm trying to use django FilerFileField as explained in the doc http://django-filer.readthedocs.io/en/latest/usage.html. It renders well with a field and a hyperlink inviting to select the file like http://localhost:8000/fr/admin/filer/folder/?_pick=file. This opens the filer admin page where I can navigate.
After file selection, how can I go back to my form page with the appropriate file selected?
I never get back such example as explained in the doc
Here is what I have in my main url pattern
urlpatterns = [
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^filer/', include('filer.urls')),
url(r'^filebrowser_filer/', include('ckeditor_filebrowser_filer.urls')),
]
I'm not using models.FileField() because I want to be able to point to my admin Filer images and files located in media/filer_public
The models.py is the same as described in the doc
from django.db import models
from filer.fields.image import FilerImageField
from filer.fields.file import FilerFileField
class Company(models.Model):
name = models.CharField(max_length=255)
logo = FilerImageField(null=True, blank=True,
related_name="logo_company")
disclaimer = FilerFileField(null=True, blank=True,
related_name="disclaimer_company")
This is my admin.py. I have created the file by myself and don't know if I need to register it somewhere in settings.
from django.contrib import admin
from .models import Athlete
admin.site.register(Athlete)
This is my models.py:
from django.db import models
class Athlete(models.Model):
firstName = models.CharField(max_length=30)
lastName = models.CharField(max_length=30)
Both files are in my project folder. I don't have any apps. When I go to url/admin/ I expect to be able to create and edit athletes, but I can only edit groups and users.
What more do I need to do to make Athletes editable in admin?
Add your module into settings INSTALLED_APPS list. Probably you forgot it (as you guess in your comment).
Django-rest-frameworks (both tutorial and documentation) indicates to register routes as prefix-ViewSet pairs like so
# rapidsms_tut/rapidsms_tut/urls.py
#> (rapidsms_tut = project name)
#> (rapidsms_tut/rapidsms_tut = main/default app)
from rest_framework import routers
from voting import views
router.register(r'choices', voting.ChoiceViewSet) # choices is the url prefix (i.e. /choices/)
However I cannot find a way out of the following error (I'm making modifications to the rapidsms tutorial I've followed, but it doesn't really matter):
NameError at /choices
name 'voting' is not defined
/.../rapidsms_tut/rapidsms_tut/urls.py in <module>, line 12
My directory structure is as follows:
rapidsms_tut
rapidsms_tut
...
urls.py
voting
...
serializers.py
models.py # here we have Choice model
views.py # here we have CHoiceVIewSet ModelViewSet
voting/models.py
from django.db import models
class Choice(models.Model):
name = models.CharField(max_length=40, unique=True)
votes = models.IntegerField(default=0)
voting/views.py
from .models import Choice
from rest_framework import viewsets
from .serializers import ChoiceSerializer
class ChoiceViewSet(viewsets.ModelViewSet):
queryset = Choice.objects.all()
serializer_class = ChoiceSerializer
I guess the issue has something to do with directory structure / namespaces. Any clue?
Also, by following Django starter tutorial, I've been taught to have the default app (with settings.py, urls.py etc.) as a sub-folder of project folder (e.g. rapidsms_tut as sub-folder of rapidsms_tut).
However I've seen projects around with different apps but with urls.py, settings.py etc only in the root folder of the project (e.g. in rapidsms_tut and not default app's subfolder). What's the difference?
The issue is not related to importing the module or your dir structure, it's because voting is not defined in your code.
from voting import views
router.register(r'choices', voting.ChoiceViewSet)
You imported the views module from voting, not voting directly. You should change the route registration line to:
router.register(r'choices', views.ChoiceViewSet)
As for the second question, It's really a matter of preference, Django doesn't enforce you to use either structure. Here's the template I usually use for my projects: https://github.com/jpadilla/django-project-template
The settings.py and the main urls.py, are in the root project dir. I then make a sub dir called apps/, where all of the apps reside
I have a simple question. This is my profile:
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
born = models.DateTimeField('born to')
photo = models.ImageField(upload_to='profile_photo')
I want to create a registration form with these fields (from User and Profile models):
username
first_name
last_name
born
photo
These fields are required.
How do I do that?
How does get_profile() work in a template for this issue?
Thank you :)
Setup
Are you using the django-profiles and django-registration projects? If not, you should—much of this code has already been written for you.
Profile
Your user profile code is:
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
born = models.DateTimeField('born to')
photo = models.ImageField(upload_to='profile_photo')
Have you correctly setup this profile in your Django settings? You should add this if not, substituting yourapp for your app's name:
AUTH_PROFILE_MODULE = "yourapp.Profile"
Registration Form
django-registration comes with some default registration forms but you specified you wanted to create your own. Each Django form field defaults to required so you should not need to change that. The important part is just making sure to handle the existing registration form fields and adding in the profile creation. Something like this should work:
from django import forms
from registration.forms import RegistrationForm
from yourapp.models import Profile
from registration.models import RegistrationProfile
class YourRegistrationForm(RegistrationForm):
born = forms.DateTimeField()
photo = forms.ImageField()
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email = self.cleaned_data['email'])
new_profile = Profile(user=new_user, born=self.cleaned_data['born'], photo=self.cleaned_data['photo'])
new_profile.save()
return new_user
Bringing it together
You can use the default django-registration templates and views, but will want to pass them your form in urls.py:
from registration.backends.default import DefaultBackend
from registration.views import activate
from registration.views import register
# ... the rest of your urls until you find somewhere you want to add ...
url(r'^register/$', register,
{'form_class' : YourRegistrationForm, 'backend': DefaultBackend},
name='registration_register'),