Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django How to catch ModelForm unique together errors
I have a ModelForm which has a unique_together constraint on certain fields. Now when I pass data to the form and call form.is_valid(), it returns False if record already exists and also if some other error occurs. So, I want to do something in case the record already exists(unique_together validation fails) and another thing in case Form.is_vaid() returns False due to some other reasons. How do I achieve this ? -
How do I test Django views when they don't return values
I'm new to Django. I've worked through some tutorials, written some models, views, forms etc. but I don't understand how they can be tested since either nothing is returned, or whatever is returned is so tightly bound it doesn't make sense in terms of the test. For example here are two view classes: class ListBlogPostView(ListView): model = BlogPost template_name = 'app/blogpost_list.html' class CreateBlogPostView(CreateView): model = BlogPost template_name = 'app/blogpost_edit.html' form_class = BlogPostForm def get_success_url(self): return reverse('blogpost-list') def get_context_data(self, **kwargs): context = super(CreateBlogPostView, self).get_context_data(**kwargs) context['action'] = reverse('blogpost-new') return context The first, ListBlogPostView, doesn't return anything. How can I ever check that this is working correctly? The second has a couple of functions but their return values are not things I can test with an assert How could I ever use a TDD approach to Django? I'm used to using nunit and MS 'unit' tests in Visual Studio, mocking etc -
Application names aren't unique, duplicates: accounts
I'm attempting to run a python manage.py makemigration command, but i'm greeted with the following error message: django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: accounts I attempted to add the following to my accounts.apps.py from django.apps import AppConfig class AccountsConfig(AppConfig): name = 'accounts' label = 'my_accounts' But, that gives me the following error message: django.core.exceptions.ImproperlyConfigured: Application names aren't unique, duplicates: accounts My security.settings.py installed apps is the following: INSTALLED_APPS = [ 'accounts', 'accounts.apps.AccountsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] Is this error message because I have 'accounts', and accounts.apps.accountsconfig in my installed_apps? How can I get AccountsConfig to be part of my installed apps to allow for my migration? Below is my applications folder structure. Folder Structure -
Query-based view that changes periodically
So i'm working on a django project for a restaurant's page and i need to make a view for a menu that changes weekly. class WeeklyChangingMenu(models.Model): introduction_date = models.DateField() options = models.CharField(max_length=500) and then i have a menu view: def menu(request): menu = WeeklyChangingMenu.objects.get(introduction_date < datetime.date.today()) return render(request, "menu.html", {"menu":menu}) and i'm looking for a way to, rather than making the same query over and over, simply making the query once a week by, say, a manage.py command to store it and reuse the info as if it was hardcoded into the view for a week which would make it a lot easier for the database. -
Python IOError:Permission denied
Trying to get this data reading script up and running. I got the following error Getting run process_data.py GoogleNews-vectors-negative300.bin loading data... data loaded! number of sentences: 10662 vocab size: 18765 max sentence length: 56 loading word2vec vectors...Traceback (most recent call last): File "E:\CNN_sentence-master\process_data.py", line 137, in w2v = load_bin_vec(w2v_file, vocab) File "E:\CNN_sentence-master\process_data.py", line 69, in load_bin_vec with open(fname, "rb") as f: IOError: [Errno 13] Permission denied: 'GoogleNews-vectors-negative300.bin' My code: def load_bin_vec(fname, vocab): Loads 300x1 word vecs from Google (Mikolov) word2vec word_vecs = {} with open(fname, "rb") as f: header = f.readline() vocab_size, layer1_size = map(int, header.split()) binary_len = np.dtype('float32').itemsize * layer1_size for line in xrange(vocab_size): word = [] while True: ch = f.read(1) if ch == ' ': word = ''.join(word) break if ch != '\n': word.append(ch) if word in vocab: word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32') else: f.read(binary_len) return word_vecs if __name__=="__main__": w2v_file = sys.argv[1] data_folder = ["rt-polarity.pos","rt-polarity.neg"] print "loading data...", revs, vocab = build_data_cv(data_folder, cv=10, clean_string=True) max_l = np.max(pd.DataFrame(revs)["num_words"]) print "data loaded!" print "number of sentences: " + str(len(revs)) print "vocab size: " + str(len(vocab)) print "max sentence length: " + str(max_l) print "loading word2vec vectors...", w2v = load_bin_vec(w2v_file, vocab) print "word2vec loaded!" print "num words already in word2vec: " … -
Reporting solution with Django and Pandas
I need help designing a reporting solution for a Django app. I need to perform reporting/calculations on data stored in the DB. My problem can be summarized as follows: Read data from about 10 different tables and load it into pandas DataFrames. Perform various calculations on the data in pandas including merge and join operations. End result is a single dataframe with 60 columns and up to 1 million rows, but most use cases is less than 10k. What I've been doing so far, is sending all this data to a JavaScript UI element that summarizes the data, allows filtering/aggregation/sorting etc. It works fine when there's less then 10k rows, but obviously at 1m rows it won't work. My idea is to create "reporting views", so that the user can still select their filters/aggregations views, but the actual filtering/aggregation/sorting happening on the server instead of their browser. Now, reading the data and performing the calculations can take a bit of time, so ideally I want to cache the result if the users wants a different view. I'm not sure what the best options are here? This is what I've thought of so far: Save the results of the calculations in … -
django app_details() got an unexpected keyword argument 'slug' error
hello I want to create a simple Django blog and I want to take mivies details from some simple movies list but I take this error : app_details() got an unexpected keyword argument 'slug' I have follow docs and I think my code is correct but anytime I take this error. models.py class app_movies(models.Model): title=models.CharField(max_length=255) slug_title = models.SlugField() urls.py url(r'^movies-details/(?P<slug>[^\.]+)/$', views.movies_details, name='movies_details'), views.py def movies_list(request): return render(request, 'movies_list.html',{'movies':app_movies.objects.filter(createddate__lte=timezone.now()).order_by('-createddate')}) def movies_details(request,slug_title): movies=app_movies.objects.all() app_movies=get_object_or_404(movies, slug=slug_title) return render(request, 'movies_details.html',{'movies':movies,'app_movies':app_movies}) html tag : <a href="/movies-details/{{movies.slug_title}}">View Project <span ></span></a> -
treating dot symbols as a username string in url
The url fowards to the right page but on the page it gives a 404 error when the username has a dot. Is there a way to handle this or should I forward to ID instead? What is the proper way to do this? url.py url(r'profile/(?P<username>[-\w.]+)$', views.get_user_profile, name='user_profile'), views.py def get_user_profile(request, username): user = User.objects.get(username=username) render (request, 'pages/user_profile.html', {"user":user}) -
Cassandra connection pooling in Django
Most of the requests to my Django application (hosted by Apache) require connecting to a Cassandra keyspace and fetching/processing data. Currently, I open a new connection per request, which adds a 2.5s overhead to even the simplest requests! The solution is to use connection pooling, but it's not very clear to me how this can be done in Django? Here is what I've found so far: I can use an object mapper like CQL-Engine or Django Cassandra Engine, which they persist the connection across multiple requests. I don't need an object mapper, so I would rather skip using a library that I don't need. Of course, I'm already using Datastax Python driver, which means I could use CQL-Engine, but it's not very clear how it can help! I can modify my WSGI and connect to Cassandra after the process fork, and share that with every request handled by that process (more details here). I'm currently using mod_wsgi, but I found more results on gunicorn (I don't mind switching). For gunicorn there is a callback, post_fork which I could use (as shown in this example). There I can connect, but I'm not sure how to share the connection with the application! … -
Total Views Count for a User
Here's the model, class Answer(models.Model): user = models.ForeignKey(User) ...... ...... views = models.IntegerField(default=0) How can I print the total number of 'views' for a specific user for all the Answer he/she have ever written? Please help me, thanks in Advance! -
Django: dynamic model fields and migrations
I have problems understanding how django's model fields work. What I want to achieve is something like a PriceField (DecimalField), that dynamically creates/injects another model field, let's say a currency (CharField) field. I have read an interesing blog posts about this topic at https://blog.elsdoerfer.name/2008/01/08/fuzzydates-or-one-django-model-field-multiple-database-columns/. I think (and hope) that I've understood the core messages of the articles. But as most of them are a little bit outdated, I don't know if they are still valid for current django versions and my below code. I use Django 1.11.4, Python 3.6.2, and a clean app created with ./manage.py startapp testing. The code in models.py: from django.db import models from django.db.models import signals _currency_field_name = lambda name: '{}_extension'.format(name) class PriceField(models.DecimalField): def contribute_to_class(self, cls, name): # add the extra currency field (CharField) to the class if not cls._meta.abstract: currency_field = models.CharField( max_length=3, editable=False, null=True, blank=True ) cls.add_to_class(_currency_field_name(name), currency_field) # add the original price field (DecimalField) to the class super().contribute_to_class(cls, name) # TODO: set the descriptor # setattr(cls, self.name, FooDescriptor(self)) class FooModel(models.Model): price = PriceField('agrhhhhh', decimal_places=3, max_digits=10, blank=True, null=True) The problems come if I try to create migrations for that models. If executing python manage.py makemigrations following message is shown: Migrations for 'testing': testing/migrations/0001_initial.py - … -
Check if merge migration is required, without database
To check if merge migrations are required, I can run manage.py makemigrations --check or manage.py makemigrations --dry-run However, both of those require the database to be up. If it's not up, it will error out with something like django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)") Theoretically, since a merge migration issue occurs because of two migration with the same parent, you don't need the database instance to be up to check for this condition. I need this because I want my CI to check for this case. I can spin up a docker database but it's extra work for something that's not even logically dependent. I'm also sure there are people out there who are interested in checking for this in their CI, who don't want to deal with containerization. Has anyone found an easy way to to check for migration merge conflicts without needing a database up? -
How to create unknown numbers of HTML tables
I am trying to render a table using Django-Python. Let us say I have a record of last names starting from A-Z. Now, I want to create a table based on each letter, and put all the people with that first-letter last name on their own table. EX: TABLE A Last names Adams Anderson TABLE B Last names Barnes Bill Brandon TABLE J Last names Jackson Johnson etc…. ## this is the output I want to achieve HTML: <legend>Customers</legend> <table> <tr> <th>First Name</th> <th>Last Name</th> </tr> <tr> {% for idx in people %} <td>{{ idx.0 }}</td> <td>{{ idx.1 }}</td> </tr> {% endfor %} </table> views.py def index(request): context = { "people": People.objects.all() } return render(request, 'index.html', context) I can easily render this. However, I am trying to separate everyone based on their last name by creating as many as 26 tables. I want them to all appear in the same html page, but separated by tables accordingly. I don’t know how many letters I have from the database and I am trying to avoid hard-coding a table with A-Z header. This is because in my app, it is possible that out of 26, I only have 5 letters available. For … -
Using a generator with Django's Paginator
Is it possible for Django's Paginator to work with a Python generator? For example, assuming I have a generator which will automatically paginate requests to an API like this pseudocode: def get_response(): page_one = requests.get('http://example.com/json?page=1') for item in page_one: yield item page_two = requests.get('http://example.com/json?page=2') for item in page_two: yield item Could Django's Paginator be extended to support this generator? generator = get_response() paginator = Paginator(generator, 10) I know this approach will work: paginator = Paginator(list(generator), 10) My understanding is that passing a generator directly would be more efficient because the API client would not make a request until it was required to do so. Though I understand that a Paginator requires a count() or __len__() method which may make this impossible. -
Mock 1-to-1 field in Django tests
I am struggling with the following question. Each User (the default Django model) has a Profile, and there is a 1-to-1 relationship between the models. Using the post_save signal we automatically create a corresponding Profile after a User was created. So what I want to do is to mock that .profile attribute during testing and replace it by a method implementing the lazy initialization/creation, e.g. something like: property(lambda self: Profile.objects.get_or_create(user=self)[0]) The final aim is simple: optimize our tests without massive refactoring (we have a lot of tests, and I am not going to tweak each specific test; what I know is that only some of them actually deal with profiles, so for most tests those profiles are not needed and only lead to redundant DB queries). -
Pagination when using LinkColumn in django-tables2
I'm using a paginated table in my app and the pagination part works great. The table contains Ticket Id and Title of a ticket. I want the Id column to point to a ticket details page. It all works great, but when i click the Ticket Id the page loads without the page query string parameter and my pagination is back on page one. Is it possible to perserve or add the page query parameter when using LinkColumn? Do I have to build a custom table template for this? urls.py url(r'^(?P<ticket_id>C[0-9]+)/$', views.ticket_view, name='ticket_view'), tables.py class TicketsTable(tables.Table): class Meta: model = IssueTicket fields = ('ticket_id', 'title', 'status', 'target_date') attrs = {'class': 'table table-bordered table-hover'} ticket_id = tables.LinkColumn('IssueTracker:ticket_view', args=[A('ticket_id')]) title = tables.Column() status = tables.Column(accessor='get_status_display') target_date = tables.Column() The result with missing query parameter <a href="/IssueManagement/C000012/">C000012</a> -
Making a RESTful API with redshift
I would like to make a website with stats and graphics and I will have many data. (The website should handle authentification and some little management). My question is: can I make an RESTful API with Redshift using Django (and DRF)? But I know it's impossible to use an ORM with Redshift. Should I use Django + RDS (PostgreSQL) as a primary database and use Redshift just to store the data? But how can I do it? And how can I retrieve this data to make a graphic? PS: I never used AWS so, if you have any advice :) Thank you. -
What is creating these Django database log entries?
My development server's Django database log files are quite large because a particular event is being logged once every minute: [10/Sep/2017 21:02:02] DEBUG [django.db.backends:89] (0.001) DELETE FROM "django_session" WHERE "django_session"."expire_date" < '2017-09-10T21:02:02.514421+00:00'::timestamptz; args=(datetime.datetime(2017, 9, 10, 21, 2, 2, 514421, tzinfo=<UTC>),) [10/Sep/2017 21:03:02] DEBUG [django.db.backends:89] (0.002) DELETE FROM "django_session" WHERE "django_session"."expire_date" < '2017-09-10T21:03:02.265801+00:00'::timestamptz; args=(datetime.datetime(2017, 9, 10, 21, 3, 2, 265801, tzinfo=<UTC>),) [10/Sep/2017 21:04:02] DEBUG [django.db.backends:89] (0.002) DELETE FROM "django_session" WHERE "django_session"."expire_date" < '2017-09-10T21:04:02.033147+00:00'::timestamptz; args=(datetime.datetime(2017, 9, 10, 21, 4, 2, 33147, tzinfo=<UTC>),) My settings file contains these session-related directives: DEBUG = True SESSION_COOKIE_SECURE = True MIDDLEWARE_CLASSES = ( # ... 'django.contrib.sessions.middleware.SessionMiddleware', # ... 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', # ... ) SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' SESSION_COOKIE_AGE = 60 * 60 # Refresh session state with every request. This effectively creates # a "sliding" session expiration. A user will remain signed in so # long as they send at least one request every SESSION_COOKIE_AGE minutes. SESSION_SAVE_EVERY_REQUEST = True INSTALLED_APPS = [ # ... 'django.contrib.sessions', # ... ] I don't have anything session-related in my LOG settings. What is causing these DEBUG entries and is there any way to disable them or at least reduce their frequency? -
Python-Django: module import syntax conflicting
I hope this is not a duplicate, I couldn't find any other answer. Going straight to the point, my problem is as follows. I have a project in Django where django-apps use external custom modules. This is the structure: Project_dir/ - core/ - module_1.py - module_2.py - django_project/ - __init__.py - settings.py - urls.py - wsgi.py - django_app_A/ - views.py - manage.py The problem is that I need to import some classes and methods of moudule_2 in module_1, and i would do so by simply, in module_1, from module_2 import foo When I run module_1 for testing, everything works fine. Nonetheless, I need to import module_1 in django_app_A/views.py, and I would do so by from core.module_1 import Bar Here's the problem: if I have another relative import in module_1, as I have, I will get a ModuleNotFoundError: No module named 'module_2' UNLESS I use in module_1 the syntax from .module_2 import foo By doing so, the Django app will work fine and page will properly load, but at the same time I "break" the module_1, as I won't be able to run it stand-alone any more, as I will get a ModuleNotFoundError: No module named '__main__.module_2' I have no idea … -
Skip one or more constraints in Django form.is_valid()
Given the following model: class User(models.Model): role = models.IntegerField(default=0, blank=True) name = models.CharField(max_length=255, blank=False, null=False) email = models.EmailField(max_length=128, unique=True, blank=False, null=False) I need that form.is_valid() would skip the unique constraint on email field. Is this possible? -
Can't verify site with LetsEncrypt
I have been trying to get an SSL certificate for a site using LetsEncrypt, "a free, automated, and open certificate authority (CA), run for the public’s benefit". There is an ACME shell script that is meant to make the process of using LetsEncrypts API easier to receive an SSL certificate. One of their methods requires you to enter in your domain name, and the webroot of your application. acme.sh --issue -d example.com -w /home/wwwroot/example.com However, when I try to enter this command, it says that it was unable to verify the domain. I added a --debug flag to the script command, and received this, but am not sure where the problem lies. [Mon Sep 11 05:05:01 UTC 2017] Using config home:/home/doc4design/.acme.sh [Mon Sep 11 05:05:01 UTC 2017] DOMAIN_PATH='/home/doc4design/.acme.sh/doc4design.com' [Mon Sep 11 05:05:01 UTC 2017] Using ACME_DIRECTORY: https://acme-v01.api.letsencrypt.org/directory [Mon Sep 11 05:05:01 UTC 2017] _init api for server: https://acme-v01.api.letsencrypt.org/directory [Mon Sep 11 05:05:01 UTC 2017] ACME_KEY_CHANGE='https://acme-v01.api.letsencrypt.org/acme/key-change' [Mon Sep 11 05:05:01 UTC 2017] ACME_NEW_AUTHZ='https://acme-v01.api.letsencrypt.org/acme/new-authz' [Mon Sep 11 05:05:01 UTC 2017] ACME_NEW_ORDER='https://acme-v01.api.letsencrypt.org/acme/new-cert' [Mon Sep 11 05:05:01 UTC 2017] ACME_NEW_ACCOUNT='https://acme-v01.api.letsencrypt.org/acme/new-reg' [Mon Sep 11 05:05:01 UTC 2017] ACME_REVOKE_CERT='https://acme-v01.api.letsencrypt.org/acme/revoke-cert' [Mon Sep 11 05:05:01 UTC 2017] Le_NextRenewTime [Mon Sep 11 05:05:01 UTC 2017] _on_before_issue [Mon Sep 11 … -
Django admin ForeignKey list filter with huge related table
I have this model: class Post(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField() user = models.ForeignKey('users.User') And this ModelAdmin: class PostAdmin(admin.ModelAdmin): list_display = ['title', 'user'] list_filter = ['user'] Filtering works well, but I faced with such problem. In my case users.User table is very big, so when Django is rendering PostAdmin page, it takes too many time to render all users. So I am wondering if it is possible to use Django Admin filter with ForeignKey without rendering all related objects, something like raw_id_fields widget for filters? -
In form tag of html, i want to extract the input given by user and send it to other page [on hold]
<html> <body> <h1>Course Sign Up Page</h1> <form action="logged_in.html" method="post"> <label for="SNo">Enter the serial number:</label> <input id="SNo" type="number" name="serial_no"><br> <input type="submit" value="Search"> </form> </body> </html> Now when i submit, how do i receive the input that is entered in Enter the serial number: area ? -
Get first form error in django views
Hi i'm having problem for getting first error of the form in Django View here is what i did views.py frm = PasswordChangeForm(request.user,request.POST) if frm.is_valid(): frm.save() else: jsonStringErrors = frm.errors.as_json() jsonErrors = loads(jsonStringErrors) old_passwordError = jsonErrors['old_password'] if old_passwordError: context["error"] = old_passwordError[0]["message"] else: new_passwordError = jsonErrors["new_password2"] if new_passwordError: context["error"] = new_passwordError[0]["message"] else: context["error"] = frm.errors.as_text() and when user enter's password correctly i get error on line 7.so id tried old_passwordError = jsonErrors.items()[0] also this old_passwordError = jsonErrors.keys()[0] but i got this error 'dict_keys' object does not support indexing Comment's are welcomed and Thanks for reading -
Include is not defined
I'm sure this is an elementary issue that i'm overlooking, but i've been struggling to get my manged.py commands to work because the following error: NameError: name 'include' is not defined. my project urls.py is the following, which does have include: from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^$', views.home, name='home'), ] my root apps.py contains: from django.apps import AppConfig class AccountsConfig(AppConfig): name = 'accounts' my security app urls.py is the following: from django.conf.urls import url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^account/', include('accounts.url')), ] my settings.py has accounts in the installed apps: INSTALLED_APPS = [ 'accounts', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] what could my issue be? File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "C:\python\security\security\urls.py", line 21, in <module> url(r'^account/', include('accounts.url')), NameError: name 'include' is not defined