Brett Jackson
> <@gitter_bopeng:matrix.org> How to I prevent users from registering user names such as user
, users
, about
, and login
? I have URL /{user_name}
mapped for users so these usernames could conflict with system URLs (/about
will not work even if a user successfully registers about
as a user name). I even want to disallow names such as fxxk
. I could list all names in clean_username
but I am wondering if there is an existing approach.
Bo, could you mitigate that by placing your other routes above the /{user_name}
route?
/users
from all_auth
to /~users
, having a user named /users
will still be confusing. I came across https://github.com/dsignr/disallowed-usernames/ and added the list to UserCreationForm.clean_username
.
/{user_names}
to admin.mycompany.com
using django-hosts but it might be an overkill and does not really solve the problem of mycompany.com/users
even if the real users
URL is admin.mycompnay.com/users
.
To avoid storing multiple copies of the same file (from multiple objects the files belong or multiple users), I would like to separate filename and file content in handling user-uploaded files. That is to say, the model will use two
char
fieldsfilename
,filecontent
(hash value) to refer to a file, where the content is saved separately according hash values in a shared location. In this way renaming files, uploading files with the same content will not result in new file content. I feel that this could be a very common approach for repository-like applications. Does anyone know an existing app or example that work in this way?
Hmm. Why don't you create a new Model called BookVersion that will store histories of books (One-to-Many)? Then, fillter them with the Book Id
Book
to selected invitees and groups so that everyone sees a different set of versions. For example, a "work in progress" versions could be shared with group members, and "published" versions could be shared to all visitors. What you suggested seems to like a Model for Book
and a one to many relationship to multiple versions of the Book, but I still cannot figure out how to get the most "viewable" version for each user using these two models.
I have some issues with Django migrations, and I'm not sure if it's intended behaviour or a bug. I have two migrations, one adds a new permission to a model, the next runs Python code to look up a group and gives that new permission to this group:
class Migration(migrations.Migration):
dependencies = [
("gallery", "0023_gallerypermissions"),
]
operations = [
migrations.AlterModelOptions(
name="gallerypermissions",
options={
"default_permissions": (),
"managed": False,
"permissions": (
(
"manage_reference_shots",
"Manually upload and remove reference shots.",
),
),
},
),
]
def add_permission(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
group = Group.objects.get(name="Regular")
group.permissions.add(Permission.objects.get(codename="manage_reference_shots"))
group.save()
def remove_permission(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
group = Group.objects.get(name="Regular")
group.permissions.remove(Permission.objects.get(codename="manage_reference_shots"))
group.save()
class Migration(migrations.Migration):
dependencies = [
("gallery", "0024_auto_20200226_1059"),
]
operations = [migrations.RunPython(add_permission, reverse_code=remove_permission)]
Now, if I run those migrations separately, all works well. If I simply run migrate
, it fails due to the second migration not being able to find manage_reference_shots
in the database. I kinda understand this, as all migrations run in the same transaction (is there a way to run them in separate transactions, without doing so manually from the command line?).
What I don't understand is: The first migration is marked as done, even though the permission is NOT in the database afterwards!
@leqnam Not really. I am doing something like
def get_queryset(self):
books = Book.objects.filter(by self.kwargs['user'])
return [x.id for x in books if is_latest_version(x, books)]
with the risk of fellow workers calling get_queryset().filter()
later so I am still hoping for a SQL/ORM solution, or something like return QuerySet(instance_list)
that can be filtered
upon.
Hello everyone,
is it possible to run something like a cron job when a model is saved? And to stop that too?
The goal is:
is that even possible with django? I read about celery but this has to be triggered by a command line if i did not get this wrong
django-email-confirmation
), and I might have to rename it (e.g. django-deferred-form
so that it can be released and used in my application (right now I have to install git
to use its github version).
django-email-confirm-la
confirms them. The last commit for this package is 3 years ago and it does not support django 2.x though.
from django.shortcuts import render
from django.views.generic import ListView
from .models import Post
#from django.http import HttpResponse
def home(request):
context = {
'posts' : Post.objects.all()
}
return render(request, 'blog/home.html', context)
def PostListView(ListView):
model = Post
template_name = 'blog/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-date_posted']
def about(request):
return render(request, 'blog/about.html', {'title' : 'About'})
from django.urls import path
from .views import PostListView
from . import views
urlpatterns = [
path('', PostListView.as_view(), name = 'blog-home'),
path('about/', views.about, name = 'blog-about'),
]
path('', PostListView.as_view(), name = 'blog-home'),
AttributeError: 'function' object has no attribute 'as_view'
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your account has been created! You are now able to log in')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance= request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance= request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'Your account has been updated!!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance= request.user)
p_form = ProfileUpdateForm(instance= request.user.profile)
context = {
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/profile.html', context)
TypeError: save() got an unexpected keyword argument 'force_insert'
TypeError at /register/
save() got an unexpected keyword argument 'force_insert'
./manage.py migrate
in CD