Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to format a pandas dataframe for use with pandas-highcharts within Django
I'm trying to use pandas-highcharts within a Django project; I'm able to produce the graph, but the data is not plotted, so I'm guessing my pandas dataframe is not formatted correctly, or maybe I'm not using the right method to render the template. The result: My dataframe: Entrée d'eau - Archimède - 0013A2004166CFCD timestamp 2016-12-23 00:05:18+00:00 29.0 2016-12-23 00:05:27+00:00 29.0 2016-12-23 00:05:37+00:00 29.0 2016-12-23 00:05:47+00:00 29.0 2016-12-23 00:05:58+00:00 29.0 My view: from django.shortcuts import render from data.models import Value import pandas as pd from pandas_highcharts.core import serialize # [...] df = pd.DataFrame.from_records( Value.objects.filter(device=a).values("timestamp", "leak_value")) df.dropna(inplace=True) # not sure about this df.set_index("timestamp", inplace=True) df.sort_index(inplace=True) df = df.truncate( before=pd.to_datetime(request.POST.get("start")), after=pd.to_datetime(request.POST.get("stop"))) df = df.rename( index=str, columns={"leak_value": "{} - {} - {}".format( Room.objects.filter(unit=unit_sel).get(device=a), Device.objects.get(address=a).devicetype, a)}) print(df.head()) # DEBUG chart = serialize( df=df, render_to='Leak Values', title="Leak Values", output_type='json') return render(request, "leak_chart.html", context={"chart": chart}) My template (I include jquery and highcharts in base.html): {% extends "base.html" %} {% block body %} {% load staticfiles %} <div id="Leak Values"></div> <script type="text/javascript"> new Highcharts.Chart({{chart|safe}}); </script> {% endblock %} The page source: https://pastebin.com/EkJYQPLQ By the way I haven't found a tag for pandas-highcharts and I don't think I have the privileges to create it. I'm using pandas-highcharts 0.5.2 -
Django enforcing unique constraint on set of members in ManyToManyField
I am working in Django and I'm having some trouble enforcing a particular constraint on my data models. I have a model that consists of a group of members. class Member: name = models.CharField(max_length=100) class Team: members = models.ManyToManyField(Member) I want to enforce a constraint such that, for any given team, the set of members is unique. That is, the following teams are allowed: team_a: {A, B, C} team_b: {A, B, D} But a team_c: {C, B, A} is not allowed because the set of members is the same as team_a. How can I enforce this constraint such that an error is thrown if I try to add team_c (or any other team with the same set of members)? I have considered having a foreign key for each member and then using unique_together, but this doesn't work because I'd like the model to be unaware of ordering within the members (as there is no ordering inherent in the data). I think this might be a job for the m2m_changed signal, but I haven't been able to get it to work properly. -
DRF APIRequestFactory error
I have a view as below, class LoginAPI(APIView): def post(self, request): """ Provides login functionality. INPUT: { "username":"user_name", "password":"secret" } OUTPUT: if successful: {'success': True, 'message': 'User authenticated.'} else: {'success': False, 'message': 'Authentication failed.'} """ details = request.data try: user = authenticate(username=details['username'], password=details['password']) login(request, user) return Response({'success': True, 'message': 'User authenticated.', 'user_id': user.id}) except: return Response({'success': False, 'message': 'Authentication failed.'}, status=status.HTTP_401_UNAUTHORIZED) and I wrote simple test case as below, class APIViewsTestBase(TestCase): def setUp(self): self.factory = APIRequestFactory() self.api_client = Client() self.username = "test_user" self.password = "test_password" self.dev = Developer.objects.create(username=self.username, is_staff=True) self.dev.set_password(raw_password=self.password) self.dev.save() class TestLoginAPI(APIViewsTestBase): def test_LoginAPI(self): url = '/api/v1/login/' data = {"username": "test_user", "password": "test_password"} request = self.factory.post(path=url, data=data, format='json') view = LoginAPI.as_view() response = view(request) self.assertEqual(response.status_code, 200) When I run the test, I always getting 401 error code. Fortunately or unfortunately while I trying with Client() which is comes with django's test module, the test is passed. I executed the same instructions in django shell but, I got the same error. (not an error, response.status_code is 401)Is there any problem with my logic or code ? Somebody please help me ? -
Django-allauth google authenticaion not working in production (apache2 + mod_wsgi)
I have deployed my django web application on my institute server using apache and mod_wsgi and I am using django-allauth google authentication. My institute network uses few proxy servers to interact with the Internet. Google authentication works fine while I am running app on localhost, but as soon as I migrate the app to https://fusion.*******.ac.in, google authentication shows following Error image callback uri: https_://fusion.*******.ac.in/accounts/google/login/callback/ Please help me with this problem. -
Fixing null value in column "date_of_birth" violates not-null constraint.
I am build an Electronic Health Record Software. One of my views contians a form that captures the patients basic information. forms.py from django import forms from nesting.models import Identity_unique class Identity_Form(forms.ModelForm): NIS = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter NIS', 'class' : 'form-control' } ) ) First_Name = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter First Name', 'class' : 'form-control' } ) ) Last_Name = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Last Name', 'class' : 'form-control' } ) ) Residence = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Address', 'class' : 'form-control' } ) ) DOB = forms.CharField( widget=forms.TextInput( attrs={ 'placeholder': 'Enter Date of Birth', 'class' : 'form-control' } ) ) class Meta: model = Identity_unique fields = ('NIS', 'First_Name', 'Last_Name', 'Residence', 'DOB',) The data shema for the modelForm models.py from django.db import models from django.contrib.auth.models import User from Identity import settings import datetime # Create your models here. class Identity_unique(models.Model): NIS = models.CharField(max_length = 200, primary_key = True) user = models.ForeignKey(settings.AUTH_USER_MODEL) Timestamp = models.DateTimeField(auto_now = True) First_Name = models.CharField(max_length = 80, null = True, ) Last_Name = models.CharField(max_length = 80, null = True, ) Residence = models.CharField(max_length = 80, blank = True ) DOB = models.DateField(auto_now = False) Unfortunately I keep getting this error … -
"JSON parse error" when sending from Ajax to Django REST Framework
I am trying to send a JSON to my restapi, builded using Django Rest Framework and all I got an error message whenever I send this request, because the other ajax call have the same structure. No matter what view I am acessing, I got the same error. I am pretty sure that the problem is on the ajax configuration or on the javascript that creates the json data, because when I send a json through 'Advanced REST client' or even through an app build using Xamarin Forms, the response is 200. function createJson( email, pass){ tmpObj = {"Email": email, "Pass": pass}; json_result = JSON.stringify(tmpObj); return json_result; } function sendLogin(){ var jsonData = createJson(State.email, State.pass); console.log(jsonData) $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/restapi/usuarios/login/', data: jsonData, processData: false , success: function(json) { console.log(json); State = json; first_name = getFirstName(State.Nome) title = "Login"; msg_html = "Bemvindo, <b>" + first_name + "</b>."; modal = createModal(title, msg_html); }, // handle a non-successful response error : function(xhr,errmsg,err) { var msg = "Oops! We have encountered an error: "+errmsg; console.log(msg); console.log(xhr.status + ": " + xhr.responseText); setState({sent: 'error', result: err, email: ''}); } }); } On the Server-Side: @api_view(['POST']) def UsuarioLogin(request): data = JSONParser().parse(request) … -
Vimeo 'Replace' API Endpoint Not Changing Thumbnail
I am using Vimeo's API for the users of my app to upload videos, or replace their existing video with a new one. I am using a Vimeo Client to help me make the calls in my Django Application. Uploading works without any issues, but when I try to replace an existing video with a new one, the thumbnail stays as the old video. If you play the video, it will play the new one, but the thumbnail never changes. Model Method that Uploads/Replaces def vimeo_upload(self): media_file = self.video_file if media_file and os.path.exists(media_file.path): v = vimeo.VimeoClient(token=settings.VIMEO_ACCESS_TOKEN, key=settings.VIMEO_API_KEY, secret=settings.VIMEO_API_SECRET) if self.video_url is None: try: video_uri = v.upload(media_file.path) except AssertionError as exc: logging.error('Vimeo Error: %s Video: %s' % (exc, media_file.path)) else: self.video_url = video_uri else: try: v.replace(video_uri=self.video_url, filename=media_file.path) except Exception as exc: self.video_url = None logging.error('Vimeo Replace Error: %s Video: %s' % (exc, media_file.path)) # set the video title, description, etc. if self.video_url: try: # convert locale from en-us form to en v.patch(self.video_url, data={'description': self.customer.full_name, }) except Exception as exc: logging.error('Vimeo Patch Error: %s Video: %s' % (exc, media_file.path)) Vimeo Client Model, and UploadVideoMixin class UploadVideoMixin(object): """Handle uploading a new video to the Vimeo API.""" UPLOAD_ENDPOINT = '/me/videos' REPLACE_ENDPOINT = '{video_uri}/files' def … -
How to get list elements from manytomany field in Django Rest Framework serializer
Following Django REST framework docs I can't get dict nested fields when posting from test client. My models are: class ClassificationOfDiseases(models.Model): code = models.CharField(max_length=10) description = models.CharField(max_length=300) abbreviated_description = models.CharField(max_length=190) parent = models.ForeignKey('self', null=True, related_name='children') def __str__(self): return self.abbreviated_description class Group(models.Model): experiment = models.ForeignKey(Experiment, related_name='groups') title = models.CharField(max_length=50) description = models.TextField() inclusion_criteria = \ models.ManyToManyField(ClassificationOfDiseases, blank=True) protocol_component = models.ForeignKey( ProtocolComponent, null=True, blank=True ) The serializers for that models are: class ClassificationOfDiseasesSerializer(serializers.Serializer): class Meta: model = ClassificationOfDiseases fields = ('code',) class GroupSerializer(serializers.ModelSerializer): experiment = serializers.ReadOnlyField(source='experiment.title') inclusion_criteria = ClassificationOfDiseasesSerializer(many=True, read_only=False) class Meta: model = Group fields = ('id', 'title', 'description', 'experiment', 'inclusion_criteria') def create(self, validated_data): group = Group.objects.create(experiment=validated_data['experiment'], title=validated_data['title'], description=validated_data['description']) if 'inclusion_criteria' in self.initial_data: inclusion_criteria = self.initial_data['inclusion_criteria'] print(inclusion_criteria) # DEBUG print(self.initial_data) # DEBUG for criteria in inclusion_criteria: classification_of_diseases = \ ClassificationOfDiseases.objects.filter( code=criteria['code'] ) if classification_of_diseases: group.inclusion_criteria.add( classification_of_diseases.first() ) return group And, the test posts data to the API as follows: def test_POSTing_new_group_adds_pre_existent_classification_of_diseases(self): owner = User.objects.get(username='lab1') experiment = Experiment.objects.get(nes_id=1, owner=owner) self.client.login(username=owner.username, password='nep-lab1') list_url = reverse('api_experiment_groups-list', kwargs={'experiment_nes_id': experiment.nes_id}) response = self.client.post( list_url, { 'title': 'A title', 'description': 'A description', # we post inclusion_criteria's that exists in # ClassificationOfDiseases table (this table is # pre-populated in db) 'inclusion_criteria': [ {'code': 'A00'}, {'code': 'A1782'}, {'code': 'A3681'}, … -
Django RedirectView with missing variables
Hello I want to redirect the urls like /{category}/{sub_category} to /{category}/ but RedirectView wants to resolve the url with both subcategory and category keyword arguments url(r'^/(?P<category_slug>[^\/]+)/$', CategoryView.as_view(), name='category'), url(r'^/(?P<category_slug>[^\/]+)/(?P<another_parameter>[^\/]+)$', RedirectView.as_view(pattern_name='category')), so this doesn't work, I think default should be ignoring unrequired parameters.. I am getting django.urls.exceptions.NoReverseMatch: What would be the best option to make this work? -
Cython error when pushing git on heroku master
I am getting these errors when I hit the command, "git push heroku master". It tells me that I have to install Cython. I had also installed using "pip3 install cython" but it still shows me an error. I have also changed my requirements.txt file too. -
django recurrence--get the last instance of a recurrence field
I need help on django recurrence. I am trying to get the next and last instance from a model recurrence field as shown: course = Course.objects.all() for c in course: occurrence = c.recurrences.occurrences() print(occurrence[0]) print(occurrence[-1]) This approach works but it is extremely slow. Here is the comment from the recurrence package author and suggests to use recurrences between two dates, but as I need the last instance which in some cases is infinite(end year as 9999) it's not helping with the performance. Is there any better alternative to fetch the next and last instances from recurring field? -
Custom logo in django jet
I am currently trying to do some customization to the skin of the django admin panel, to make it more in line with our brand. Currently we use django-jet to spruce up the admin panel. Are custom css/html possible with django-jet? All of the comments say that I should change some html files, but I think those files are hidden from my project because django-jet takes care of them? If anyone could point me in the right direction it would be appreciated. Thank you -
django-admin-tools 0.8 after instaling and configurating shows error 404 while trying to open admin page
I had installed and configured django-admin-tools 0.8 following the documentation. Django version is 1.11.4, using virtualenv in project. File settings.py: INSTALLED_APPS = [ 'admin_tools', 'admin_tools.theming', 'admin_tools.menu', 'admin_tools.dashboard', 'django.contrib.auth', 'django.contrib.sites', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'atelierapp', ] TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], #'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.core.context_processors.request', ], 'loaders': [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'admin_tools.template_loaders.Loader', ] }, }, ] File urls.py: urlpatterns = [ #url(r'^admin/', admin.site.urls), url(r'^admin_tools/', include('admin_tools.urls')), ] But while I'm trying to enter the admin panel, there is error 404. Screenshot What is the problem I don't see? Thanks for your time. -
Django for Messaging Website, Good it Bad
Is using Django a good option to make a messaging website, coz I'm already developing other apps on Django. Will it be slower, use more processing power or anything related to that. If not Django, what framework should I use. I'm new to this framework so any kind of help would be welcomed. Thank You -
Django reverse query filter
I am trying to filter a query I have in my view. Model relation looks more or less like this: class User(Model): username = TextField() class Period(Model): date = DateField() class Bill(Model): period = ForeignKey(Period) user = ForeignKey(User) I have a list of users and I want to filter them based on whether they have a bill in certain period. I went through the docs found out that I have to make some kind of reverse lookup and came up with something like: queryset.filter(bill__period__date__month=datetime.datetime.now().month) I get the queryset from some super() call and its output is of UserQuerySet type, co I guess this one is right. However, when I check this. Also, when while debugging I make: Bill.objects.filter(period__date__month=datetime.datetime.now().month) I get a bill with a relation to user which would mean that at least one Bill with a user and thus a user from the queryset should be returned as a result. What am I missing? -
Django - databse out of synce?
When i run python manage.py syncdb i get some tables/models that are not synced like: Not synced (use migrations): -filmdb - musicdb - imagedb But when i do manage.py migrate filmdb it says that there's nothing to migrate. I think that might be the root cause for me getting the error: IntegrityError: duplicate key value violates unique constraint "database_pkey" DETAIL: Key(id)=(17523) already exists. Whenever i try to edit something through the admin panel. I also tried using migrate --fake and it didn't help. What can i do? -
Passing query string to ModelForm hidden field - Django
I want to pass a query string e.g., ?refcode='A1234' to a hidden field called inbound_referral_code in a ModelForm. My model is as follows: class User(models.Model): email = models.EmailField(max_length=255, blank=False, unique=True) inbound_referral_code = models.CharField(max_length=255) My ModelForm is currently as follows: class UserForm(forms.ModelForm): model = User fields = ['email', 'inbound_referral_code'] widgets = {'inbound_referral_code': forms.HiddenInput()} My View is: def register(request): if request.method == 'POST': form = UserForm(request.POST) [...] else: form = UserForm() return render(request, 'register.html', {'form': form}) And my template is currently: <form action="{% url 'register' %}" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit"/> </form> Two questions: How do I assign ?refcode parameter to inbound_referral_code field? What happens if ?refcode isn't provided? -
Django not showing up in requirements.txt
I have a repository that contains a web app powered by Django. Since I have done everything in a virtual environment, I figured pip freeze > requirements.txt would result in django being mentioned in requirements.txt. But no. Here is what it looks like: mongoengine==0.13.0 pymongo==3.5.1 python2==1.2 pytz==2017.2 six==1.10.0 No django. And django is installed in the right place, as you can see me prove here: >>> import django >>> django.__file__ '/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/__init__.py' The project is in this location: (workout) Sahands-MBP:workout sahandzarrinkoub$ ls README.md include pip-selfcheck.json sketches bin lib requirements.txt workout (workout) Sahands-MBP:workout sahandzarrinkoub$ pwd /Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout Why isn't django included in requirements.txt? How can I trust that pip freeze will produce the correct requirements for the project? EDIT: Another thing I noticed is that python3 isn't included in requirements.txt either, even though that is what python in my virtualenv points to. -
Understanding relations in django models fields
I get all fields of some model MyModel._meta.get_fields() and then I process them and want to understand which of them added by myself and which by django. For example if I have class Moovie and class Comedy which have moovie = models.ForeignKey(Moovie, on_delete=models.CASCADE), when i will get fields Moovie._meta.get_fields() field moovie will be in this list too, and if other models link to Moovie model, moovie field will be in list several times. I need skip them. -
Saving value of model field every month in Django
I need to save every month the value of a field of my model. I've seen Celery for scheduled tasks, but I was wondering if there is an other way to do it? My code: class Sales(models.Model): ... sales_objective = models.IntegerField(blank=True, null=True) sales_objective can change from one month to an other, and I would like then to create some stats where I can show: Sales objective for January: 123 Sales objective for February: 456 etc... -
Django - model instance being saved (or not)
I have two Django models, ModelA and ModelB. The latter has a foreign key link to the former. class ModelA(models.Model): item = models.BooleanField(default=True) class ModelB(models.Model): modela = models.ForeignKey(ModelA) answer = models.SmallIntegerField(null=True, blank=True) In the production code, an instance of ModelA is saved. During the same function, it seems to automatically save an instance of ModelB, as this is accessed in the template. def view(request): a = ModelA() a.item = True a.save() b = ModelB.objects.filter(modela_id=a.id) return render(request, 'template.html', context=locals()) Firstly, although this is how I want the code to work, I am not sure how the instance of ModelB is saved. (This is inherited code - I can't find a signal or call to ModelB anywhere else in the codebase). Secondly, this behaviour has stopped working in local development (but still works in production, with identical code); ModelB is not saved, so b returns None. I am pretty sure it's not a code issue as old branches of the code have the same problem. I have tried restoring my local db to a previous version, to no avail. The only thing that changed recently is that I squashed a load of migration files. Can anyone help with these questions? I'm running … -
Functional Testing with django-haystack and Elasticsearch?
I'm using django-haystack to implement search functionality in a Django app with Elasticsearch. How can I run functional tests of my app's search functionality with selenium, given that the model instances that are added in the tests aren't indexed? Is there some way to mock an Elasticsearch index, or another accepted way of artificially creating indexed results? -
how to add cms.urls integrated in my django application to sitemap.xml
I have this in my django project : In views.py : class MediatorViewSitemap(Sitemap): changefreq = 'monthly' priority = 0.8 def items(self): return Mediator.objects.exclude(photo='') def lastmod(self, obj): return obj.modified static_list =[ 'home', 'mediators_list', 'about', 'faq', 'pricing', 'terms', 'privacy', 'contact', ] class StaticViewSitemap(Sitemap): priority = 0.5 changefreq = 'daily' def items(self): return static_list def location(self, item): return reverse(item) And this in my urls.py sitemaps = { 'mediators': MediatorViewSitemap, 'static': StaticViewSitemap } url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), And it generate very well my sitemap.xml Now i have integrated djangocms to my django project, so i have this url in the same urls.py : url(r'^blog/', include('cms.urls')), What i want is to add it to the same sitemap.xml, any suggestion of any tutorial or help from anyone ? -
Disable SSL verification Sentry Raven in Django
I want to add a test sentry instance that has a self signed certificate. The app has the default RAVEN_CONFIG RAVEN_CONFIG = { 'dsn': 'https://xxxx@sentry.tst2.server.com/2', # If you are using git, you can also automatically configure the # release based on the git info. 'release': raven.fetch_git_sha(os.path.dirname(os.pardir)), } I tried to add 'verify_ssl':0 to the configuration dictionary to no avail. This is the error I'm getting: Traceback (most recent call last): File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/transport/threaded.py", line 162, in send_sync super(ThreadedHTTPTransport, self).send(data, headers) File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/transport/http.py", line 47, in send ca_certs=self.ca_certs, File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/utils/http.py", line 62, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 449, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/opt/apps/.virtualenvs/palantir/local/lib/python2.7/site-packages/raven/utils/http.py", line 46, in https_open return self.do_open(ValidHTTPSConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open raise URLError(err) URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> -
Cron job with django
In the README.md of a Django project of which I have been part of the team recently, we have the way to define a cron job : Cron deployment/cron.template # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH={{ venv_dir }}/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin * * * * * root python manage.py cron --minutely 01 * * * * root python manage.py cron --hourly 02 4 * * * root python manage.py cron --daily 22 4 * * 0 root python manage.py cron --weekly 42 4 1 * * root python manage.py cron --monthly 00 0 1 1 * root python manage.py cron --yearly @reboot root python manage.py cron --reboot # 06:00h AM: debits 00 6 * * * root $PYTHON_BIN manage.py cron --debits # 11:45h AM: deposits 45 11 * * * root $PYTHON_BIN manage.py cron --deposits Defining cron jobs Defining cron job is simple, just create a cron.py file in the loanwolf app and define your jobs using the desired interval as …