Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django 1.8: non-deterministic "FieldError: Cannot resolve keyword" on M2M field
I sometimes (once per a couple months) get a problem with a reverse lookup on a M2M relation that suddenly stops working. The M2M field is on a dynamically created model (created only once on startup, by a custom model metaclass). Here is the line responsible for the M2M field: 'tag': models.ForeignKey(model, related_name='items') (one field on an attrs dict passed as the third argument to type constructor). The FieldError is raised by queries like Foo.objects.filter(items__bar='baz'). Now, this never happens right after the restart. It attacks with surprise and starts messing up requests to a gunicorn worker (all requests with affected queries raise the error). Restarting the application helps. Obviously I can't reproduce it. Sometimes it fixes itself after an hour or two. I've read many threads with the same exception, but none of them seems to be related to my problem. -
How to create a webpage dashboard to show graphs and tables with writing minimal HTML/CSS?
How to create a simple application dashboard for a python applications? I have previously written a small flask site however I am not an expert when it comes to html/css. What is the simplest way to present data in a table and graph formats on a self hosted, internal server? -
Data Transfer Between Loosely Coupled Modules of an Application
Let's say we have a rather large project written in Python using the Django framework that is made up of multiple modules (proper term in Django is a project made up of multiple apps, but for the sake of generality we will use the term "modules"). This is to achieve low-coupling for easy maintainability (among other advantages). I'm interested in how, during the project runtime, how data would be moved between these modules. As objects? Or as strings? If data is transferred between modules in the format of an object (as is the case inside the module itself) then low-coupling cannot be achieved. The reason is because one module will be dependent on the object class located in the other module. For example, the module cannot be easily reused in a totally different project because of the dependency on another module. But if the data is in the format of strings, then low-coupling can be achieved. Separation of duties will be clear, troubleshooting will be limited to a single module, and modules can be easily reused in totally different projects. This is basically how an API sends data to a client. But is this the proper way of achieving low-coupling … -
Why to use 'self' foreignkey in Django Models?
I've seen this pattern many times but I am not sure why people use it and would like to learn. Here is small copy paste on what I mean: Category class is a blog post category which has the foreign key relationship to self. What do I gain for having such a relationship? class Category(models.Model): name = models.CharField(max_length=32) slug = models.SlugField(max_length=32) parent = models.ForeignKey('self', blank=True, null=True) Here is the post entry for the same model. Here I find it obvious you would want to have entry category as key or user if this blog had user system. class Entry(models.Model): title = models.CharField(max_length=64) slug = models.SlugField(max_length=32, default='', blank=True) created = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True) content = models.TextField() category = models.ForeignKey(Category) -
Django REST Framework. Best way to export XLS files.
There is great solution for Django REST Framework, which allows to export CSV files: https://github.com/mjumbewu/django-rest-framework-csv. There is something similar to generate XLS exel format? Thank you! -
Model Validation for internal only used model
I have a model that is only used internally. There is no form involved. It looks like this class ShareLink(models.Model): from_share = models.ForeignKey(Share, on_delete=models.CASCADE, related_name='from_share') to_share = models.ForeignKey(Share, on_delete=models.CASCADE, related_name='to_share') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) creator = models.ForeignKey(settings.AUTH_USER_MODEL) active = models.BooleanField(default=True) mode = models.PositiveSmallIntegerField(choices=SHARE_LINK_MODE_CHOICES, default=1) class Meta: unique_together = ('from_share', 'to_share', 'mode', 'active') def clean(self): ret = super(ShareLink, self).clean() return ret Depending on the colum mode i have some restrictions. eg the creator must be the creator of from_share and so on. So basically what would be done in the clean() of the model. But this is only called if a ModelForm is involved. But I do not have one as this model is only used internaly. What is the best way to validate my model ? Any advice -
how to import a module in django html?
I have a django blog. i need to use enchant to check if the text in the blog post is english, then use the api in text error correction. I installed the api on the virtual envieronement of the django blog pip install pyenchant project, and included it on the installed apps, but in the blog base.html, i tried to load it and use it's fucntions to check whether the post title is in english or not, but i did nothing. How to solve this? here is my html code: {% load enchant %} {% dictionary = enchant.Dict("en_US") %} {% dictionary.check(post.title) %} There is no errors when i run the server, but there is nothing on the html page. Note: according to the API, it's supposed to be True of False in a paragraph tag. "i tested it in the python shell." -
python gettext fails to translate EDX project
I am working on open EDX project which is built by django 1.8. Currently i am working on localization and I'm translating it to a new language. Both the Source and target languages are Unicode encoded. Altough i have translated, gettext fails to load some expressions. By that i mean in PO file i provided the correct translation but for example the original phrase is loaded in the menu. This is the mentioned line: <a href="${certificates_url}">${_("Certificates")}</a> In the file: header.html In that example Certificates is shown instead of its translation. Any idea why it happens ? And how to track this issue. -
retrieve data from filled-form then save into database and then display in table form DJANGO
I am relatively new to Django. I would like to save data from a filled-form, which is the data being filled into timesheet.html into Database then retrieve data and display it into a table which is list_timetable.html. But i am stucked and will get Attribute error on views.py "query_results = ltimesheet.objects.all()". timesheet.html {% extends 'hrfinance/base.html' %} {% block title %} Timesheet {% endblock %} {% block link %} {% load staticfiles %} <link rel="stylesheet" href="{% static 'hrfinance/css/timesheet.css' %}"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> /*to ensure that all the textbox and checkbox have been filled before users can submit*/ function validation() { /*ensure start date has been entered*/ if (!$('#sdate').val().trim()) { alert('Please fill in start date field'); } /*ensure end date has been entered*/ if (!$('#edate').val().trim()) { alert('Please fill in end date field'); } /*ensure checkbox has been ticked*/ if (!$('#agree').is(':checked')) { alert('Please indicate that you have satisfied all the requirements'); } else{ console.log('ok') } } </script> {% endblock %} {% block body %} <div class="sub-bar"> <p>Submit Timesheet</p> </div> <br> <br> <br> <br> <br> <br> <form onsubmit="return validation()"> <div class="content-wrapper"> <div class="sub-content"> <div> <p>Start Date: {{timesheet.startDate}}</p> <input id="sdate" type="date" name="startdate"> </div> </div> <div class="sub-content"> <div> <p>End Date: {{timesheet.endDate}}</p> <input id="edate" type="date" name="enddate"> </div> … -
LDAP Connection not sustained when required to call another view in Django
I'd like to direct the user to a login page. The user logs in, and his credentials are checked against an LDAP server. Once logged in the user is then redirected to a search page, where the user searches for a specific user (here is where I get stuck). Once the user hits search, the next page displays the OU, DC, etc of the user he searched for. Independently of Django, I tested my LDAP connection and search, and this worked fine. I also transferred this code from my Eclipse environment to my Visual Code environment and hardcoded the searched username, so temporarily bypassing the ldap_search.html. This also worked fine. There is a loosly similar post to this problem, but it doesn't quite solve my own issue. I'm on a Windows 10 PC, using Python3, and using the ldap3 module to build my Backend authentication. The view functions take a HTTP request, and within my ldap_authentication view, I'd like to redirect the User to a Search page once his credentials have been verified as being correct. The only problem is, my conn object is not defined in the ldap_search view, and so the LDAP authentication is not maintained: NameError at … -
Django validate user input with generic listview filter
So I'm making a really simple filter based on a generic listview. I use "GET" to retrieve a user input from a searchfield/ filterfield. I want to filter my data on "arrival_date", a DateField in models.Delivery. So this filter works but any given number will be compared to the contents of "arrival_date" What i want to do is validate the user input, it can only contain numbers and the characters "-" or "/". Besides the characters i also want to say the query needs at least four digits to be a valid input. In a regular form I'd use "clean_arrival_date" to use any validation but I need a push in the right direction to apply the same sort of validation required in this listview... Thanks for the help, Kevin Below; the views.py containing the overriding listview with query_set #views.py """ Date selector based on ListView """ from django.views.generic import ListView from .models import Delivery class DeliveryDateListView(ListView): model = Delivery template_name = 'data/deliveries_by_date.html def get_queryset(self): # Fetch the queryset from the parent get_queryset queryset = super(DeliveryDateListView, self).get_queryset() # Get the data GET parameter date = self.request.GET.get("date") if date: # Return a filtered queryset return queryset.filter(arrival_date__icontains=date) # Return the base queryset return … -
Why is my Python Django logging file empty when my console log is not?
I am trying to set up a Django log to file. Based on https://stackoverflow.com/a/19257221/214742 I came up with the following configuration: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'simple': { 'format': '%(levelname)s %(message)s' }, }, 'handlers': { 'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', 'formatter': 'simple' }, 'applogfile': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': os.path.join(PROJECT_ROOT, 'MODELINGWEB.log'), 'maxBytes': 1024*1024*15, # 15MB 'backupCount': 10, 'formatter': 'simple' }, }, 'loggers': { '': { 'level': 'DEBUG', 'handlers': ['console','applogfile'] }, }, } Now when I try to run and load a page my console log looks like: Performing system checks... System check identified no issues (0 silenced). June 28, 2017 - 10:18:22 Django version 1.11, using settings 'Modeling.settings.dev' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [28/Jun/2017 10:18:27] "GET / HTTP/1.1" 200 12564 I also get a file MODELINGWEB.log but it is empty. Why is that? I was expecting it to contain the same things... -
django - get query range from url
I use python and django I have this url http://localhost:8000/index/price=100+-+100000 the range here is between 100 and 100000 how to get and work with this range in python? -
how to set filters in column values of a table according to user requirement using django/python
If table is 'product' and column is 'subjects'.After fetching all the values of subjects.how to set filters as requested by user?I am using Django/python -
(Not all arguments converted during string formatting in python)
import MySQLdb db = MySQLdb.connect("host","root","password","db" ) cursor = db.cursor() model=Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz mode=("INSERT INTO machinedetails (ModelName) VALUES (%d)") cursor.execute(mode,model) data = cursor.fetchone() print (data) db.close() Traceback (most recent call last): File "/LLT/PRJ/btd/dev/sachin_dubey/new/System_Info.py", line 149, in cursor.execute(mode,model) File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute query = query % tuple([db.literal(item) for item in args])\ TypeError: not all arguments converted during string formatting -
get form input value from form_class in views
How do I access the company_id field in form input in the class based views forms.py class CompanyProductForm(forms.Form): company_id = forms.IntegerField() I tried overriding the form_valid() but didn't work out. view.py class CompanyProduct(LoginRequiredMixin, UserPassesTestMixin, FormView): template_name = 'company_product.html' form_class = CompanyProductForm products = [] def test_func(self): return self.request.user.is_staff def post(self, request, *args, **kwargs): self.products = [] def form_valid(self, form): id = form.cleaned_data['company id'] return super(CompanyProduct, self).form_valid(form) obj = Product.objects.filter(company_id=id) return render(request, 'product_download.html',{'product':self.products, 'num':len(self.products)}) -
django-chunked-upload freezing
Python: 3.4.6 OS: CentOS6 (also happens in *7) Package: django-chunked-upload Using the same sample from here, not always but sometimes during the chunks upload (doesn't matter the file size, happens for small or big files), the process simply frozen and nothing happens, forcing the user itself to start again. Chunked part size ("offset") is set to: 1000000 bytes Anybody have any idea what could cause this bahavior and how to solve this? Thanks! -
Side effect of the Django form method `is_valid()`: intance updated?
In views.py, I update an article instance as follows: def articleUpdate(request, articleId): ... articleToUpdate = get_object_or_404(Article, id=articleId) ... # POST articleForm = ArticleForm(request.POST, instance=articleToUpdate) print(articleToUpdate) if not articleForm.is_valid(): return render(request, template, {...}) print(articleToUpdate) articleForm.save() ... The second printing of object articleToUpdate shows that it is changed. Is this a side effect of the method is_valid()? What's going on? -
error running server Django syntax error
I'm trying to get through a Django tutorial and I'm running into a syntax error when I try to run manage.py runserver. The error is below. (C:\Users\Scott\Anaconda3) C:\Users\Scott\Desktop\django tutorials\mysite>manage.py runserver Performing system checks... Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001705F6D48C8> Traceback (most recent call last): File "C:\Users\Scott\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper fn(*args, **kwargs) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run self.check(display_num_errors=True) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\management\base.py", line 359, in check include_deployment_checks=include_deployment_checks, File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\management\base.py", line 346, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config return check_resolver(resolver) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver return check_method() File "C:\Users\Scott\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 254, in check for pattern in self.url_patterns: File "C:\Users\Scott\Anaconda3\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\utils\functional.py", line 35, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Scott\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module return import_module(self.urlconf_name) File "C:\Users\Scott\Anaconda3\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) 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 674, in exec_module File … -
ConnectionError in urllib3.connection.HTTPConnection while posting from Django admin
Error info: Exception Type: ConnectionError at /admin/items/country/add/ Exception Value: ConnectionError(: Failed to establish a new connection: [Errno 61] Connection refused) caused by: NewConnectionError(: Failed to establish a new connection: [Errno 61] Connection refused) I am getting this connection error while saving the post from Django admin. I have a MYSQL database with settings DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mystic_db', 'USER': 'root', 'PASSWORD': '*****', 'HOST': '', 'PORT': '', I am a newbie to Django Python. kindly request a solution to this issue ? -
convert time before 1900 into millisecond python?
how can I convert the time before 1900 into millisecond.currently using this method:- if 'dob' in json_data['data']: datetimestring = str(datetime.fromtimestamp(json_data['data']['dob'] / 1000.0).strftime("%d-%m-%Y")) else: datetimestring = [] result_data_for_editing = json_data['data'] and i am gettingt this error:- year=1084 is before 1900; the datetime strftime() methods require year >= 1900 -
How to create ElasticSearch mapping?
I created mapping in my views.py file (Django FrameWork): relevant_restaurants_mapping = client.suggest( index='my_restaurants', body={ 'settings': { 'number_of_shards': 5, 'number_of_replicas': 1 }, 'mappings': { 'restaurants': { 'properties': { 'title': { 'text': search_query, 'completion': { 'size': 15, 'field': 'title', 'fuzzy': { 'fuzziness': 2 }, 'analyzer': 'standard' } } } } } } ) 'title' is a field from models 'restaurants' (my search logic have to search for restaurants by their titles) but the problem is, im getting this error: TransportError(400, u'unknown_named_object_exception', u'Unknown SuggestionBuilder [restaurants]') What am i doing wrong? Thanks for your time -
Django To upload and read and write large excel file
I am new to Django and my app need to user to upload the excel files. On server side i am reading the excel file by each cells, append some values and then translate the values and again write back to excel file and download the attachment. I am able to perform this action for small files but for large file it gives me timeout error. Please see the below . enter code here def translatedoc(request): data="" convrowstr="" if request.method=='POST': response = StreamingHttpResponse (content_type='application/vnd.ms-excel') try: form=fileUpload(request.POST,request.FILES) if form.is_valid(): input_file=request.FILES.get('file') sl=request.POST.get('fsl') if sl=="Detect Language": sl="auto" else: # get sl code from database sl=languagecode.objects.filter(Language=sl).values_list('code') sl=str(sl[0][0]) # get tl code from database tl=languagecode.objects.filter(Language=request.POST.get('ftl')).values_list('code') wb = xlrd.open_workbook(file_contents=input_file.read()) wb_sheet=wb.sheet_by_index(0) for rownum in range(0, wb_sheet.nrows): convstr="" for colnum in range(0,wb_sheet.ncols): try: rw=wb_sheet.cell_value(rownum,colnum) if type(rw)==float or type(rw)==int: convstr=convstr +'<td>' + str(rw) else: convstr=convstr +'<td>' + rw except Exception as e: pass if len(convstr) + len(convrowstr) >20000: # translate if the length of doc exceed the limit #call google api module data=data + translate(convrowstr,sl,str(tl[0][0])) convrowstr="" if rownum==wb_sheet.nrows-1: convrowstr= convrowstr + "<tr>" + convstr # translate for first or last #call google api module data=data + translate(convrowstr,sl,str(tl[0][0])) convrowstr="" convrowstr= convrowstr + "<tr>" + convstr log.error(rownum) if len(data)>1: sio=StringIO.StringIO() try: … -
Django Rest Framework UniqueValidator throw error when update an item with old data
I have a serializer class: class AdministratorCreateUpdateSerializer(ModelSerializer): class Meta: model = Administrator fields = [ 'username', 'email', 'password', 'first_name', 'last_name', ] username = serializers.CharField( source='user.username', validators=[UniqueValidator(queryset=User.objects.all())] ) email = serializers.EmailField( source='user.email', validators=[UniqueValidator(queryset=User.objects.all())] ) password = serializers.CharField( source='user.password', allow_blank=True, style={'input_type': 'password'} ) first_name = serializers.CharField( source='user.first_name' ) last_name = serializers.CharField( source='user.last_name' ) When I create new administrator, the validator of username and email work well. But when I update data. I just simple fill old data and save, but the validator say that username and email must be unique. How can I change this validator only execute when updating with new value that not equal the old one? -
Attempt to get data from local database using Django and Ajax returns 404 error
I am trying to get data from my local database using Django and Ajax. I set up a view where the user can get data and can pass in arguments to filter the data: start date, end date and site id. When I test if I can successfully get the data from the database I am getting a page not found 404 error: Page not found (404) Request Method: GET Request URL: http://0.0.0.0:8000/acoustic_viz/get-data/?date_start=2016-10-28&date_end=2016-10-30&site_id=HENTONAOP Error Using the URLconf defined in acoustic_viz.urls, Django tried these URL patterns, in this order: ^admin/ ^get-data/ ^get-sites/ ^$ ^static/(?P.*)$ The current URL, acoustic_viz/get-data/, didn't match any of these. Is it because of the arguments that I passed? If so how can I change my urls.py so that Django is able to find the url with arguments? Here is my code acoustic_viz/index.html var dateStart = {key:'2016-10-28'}; var dateEnd = {key:'2016-10-30'}; var siteID = {key:'HENTONAOP'}; $("#test").on('click',function(){ console.log("test"); $.getJSON('/acoustic_viz/get-data/', {date_start: dateStart.key, date_end:dateEnd.key, site_id: siteID.key }) // when the data comes back from the server .done(function(data) { console.log(data); }); }); acoustic_viz/acoustic_viz/urls.py from django.conf.urls import url from django.contrib import admin from django.conf import settings from django.conf.urls.static import static import queries.views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^get-data/', queries.views.get_data), url(r'^get-sites/', queries.views.get_sites), url(r'^$', queries.views.hello_peeg), …