django-2.0

Django 2.0 Populating Database with a Script

空扰寡人 提交于 2020-03-03 19:16:08
问题 I'm trying to populate my Django database with a script so I don't have to enter in the data manually. So far I have the following: from my_app.models import ModelInApp import django django.setup() def add_data(data1, data2): d, created = ModelInApp.objects.get_or_create(data1=data1, data2=data2) print("- Data: {0}, Created: {1}".format(str(d), str(created))) return d def populate(): # data is a list of lists for row in data: data1 = row[0] data2 = row[1] add_data(data1, data2) if __name__ ==

What is incorrect with my urls.py file? Chapter 18 of Python Crash Course

梦想与她 提交于 2020-02-06 08:21:27
问题 Why does the python code below crash my website? But the code at the very bottom does not crash the website Here is the code that crashes the website: from django.urls import path, include from django.contrib import admin urlpatterns = [ path('admin/', admin.site.urls), path('', include('learning_logs.urls')), ] Here is the code that does not crash: from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ] Thank you 回答1: You have a space

json.dump not converting python list to JS array

烈酒焚心 提交于 2020-01-25 09:38:05
问题 When I attempt to pass a python list through to JavaScript in the template it doesn't parse the list into an JS array as expected but instead returns this ["Groceries", "Clothing", "Takeaways", "Alcohol"] causing the page to break. view.py def labels(): category_labels = [] for item in Purchase.objects.order_by().values_list('type', flat=True).distinct(): category_labels.append(item) return category_labels def index(request): try: purchases = Purchase.objects.all().order_by('-time') total

Python Django 2 Email Verification on User SignUp

≡放荡痞女 提交于 2020-01-25 08:46:04
问题 I'm working on a project using Python(3.6) and Django(2.0) in which I need to verify the user's email on registration. Here's what I have tried: App to users named as users forms.py class SignUpForm(UserCreationForm): first_name = forms.CharField(max_length=30, required=False, help_text='Optional.') last_name = forms.CharField(max_length=30, required=False, help_text='Optional.') email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.') class Meta: model =

migrating problems when porting django project to python 3 and django 2

孤街浪徒 提交于 2020-01-21 13:57:23
问题 I've been porting a Django project to python 3 and Django 2. I have had to add on_delete to all my models with foreign keys as required in Django 2. Now I have tried to make migrations for those changes have been getting TypeError: __init__() missing 1 required positional argument: 'on_delete' the file it references is the 0002 migration file not the models file which has been updated. I am not sure how to go about fixing this. I have tried faking the migrations and I still get the same error

migrating problems when porting django project to python 3 and django 2

*爱你&永不变心* 提交于 2020-01-21 13:56:29
问题 I've been porting a Django project to python 3 and Django 2. I have had to add on_delete to all my models with foreign keys as required in Django 2. Now I have tried to make migrations for those changes have been getting TypeError: __init__() missing 1 required positional argument: 'on_delete' the file it references is the 0002 migration file not the models file which has been updated. I am not sure how to go about fixing this. I have tried faking the migrations and I still get the same error

Model.ManyToManyField.all() gives AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'

北战南征 提交于 2020-01-15 11:49:26
问题 I'm using Django 2.0.2, Python 3.6.4 and PyCharm 2017.3.3 Models: (in models.py) class Position(models.Model): title = models.CharField(max_length=50) gang = models.ForeignKey(Gang, on_delete=models.CASCADE) description = models.TextField(max_length=20000) def __str__(self): return str(self.title) + ', ' + str(self.gang) class Application(models.Model): positions = models.ManyToManyField(Position) applicant = models.ForeignKey(User, on_delete=models.CASCADE) class Ranking(models.Model):

Django SECRET KEY error

放肆的年华 提交于 2020-01-15 07:25:27
问题 I'm building a little Django REST API. I've configured a separate settings module for local and production but I'm having an annoying error which is: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty. This error doesn't make sense because I have the SECRET_KEY variable set. Here are my base.py (which is my base settings for the API) and the local.py. Also, here are my wsgi.py and manage.py. base.py """ https://docs.djangoproject.com/en/2.0/ref/settings/ """

Django REST Framework URLs with Django 2.0

偶尔善良 提交于 2020-01-12 07:40:11
问题 I'm trying to set up Django REST Framework with Django 2.0 project which means url(r'^something/' ... has been replaced with path(something/ ... . I'm trying to work out how to set up my rest_framework patterns. This is what I have: router = routers.DefaultRouter() router.register(r'regulations', api.RegulationViewSet) router.register(r'languages', api.LanguageViewSet) urlpatterns = [ ... path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), ... ] If I go to http:/

Python - Django signup for custom user model with multiple types of users

心已入冬 提交于 2020-01-05 04:09:08
问题 I'm working on a project using Python(3.7) and Django(2.2) in which I have created multiple types of users with a custom user model by extending the AbstractBaseUser model. I have done with the login but I'm confused about how should I implement the signup page as there's multiple types of users and I need a different form for each type of user. Here's How I have implemented my models. From models.py : class UserManager(BaseUserManager): def _create_user(self, email, password, is_staff, is