Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to get name of folder in uploaded tar.gz file in Django?
For example, a folder named "abcd" is created and it is compressed to abcd.tar.gz. Then it's name is changed from abcd.tar.gz to xyz.tar.gz and uploaded in filefield in website. How can I get the name of folder in uploaded tar.gz file? What function should I write in my forms to get it as a string? This is my assignment import form: class AssignmentImportForm(forms.Form): assignment_archive = forms.FileField( required=True, label="Assignment Archive", help_text="Please upload the assignment archive that follows the structure given in the above sample archive. Accepted archives are tar, tar.gz." ) def __init__(self, *args, **kwargs): kwargs['error_class']=DivErrorList super(AssignmentImportForm, self).__init__(*args, **kwargs) I want to write a check function in that class to check the length of name of folder in uploaded tar.gz file (in above example, I want to check the length of folder 'abcd' i.e. 4). How can I do it? -
Pandas Privot Table from a Django-Pandas queryset
I have the following dataframe that was created from a Django queryset using Django-Pandas. I think this is important because I can't find a way to set the column index on conversion. df1 = pd.DataFrame({ 'J_col': ['us', 'us', 'us', 'ca', 'az', 'az'], 'def_desc': ['item1', 'item2', 'item3', 'item4', 'item5', 'item3'], 'desc_bal': [45000, -120000, 53000, 100, 1000, 2000], 'valid': [False, True, True, False, True, True]}) In [12]: df1 Out[12]: J_col def_desc desc_bal valid 0 us item1 45000 False 1 us item2 -120000 True 2 us item3 53000 True 3 ca item4 100 False 4 az item5 1000 True 5 az item3 2000 True I would like to have a multi-index for the columns that looks like this. def_desc us ca az desc_bal valid desc_bal valid desc_bal valid 0 item 1 45000 False 1 item 2 -120000 True 2 item 3 53000 True 1000 True 3 item 4 100 False 4 item 5 2000 True 5 Note that there will be rows with common def_desc that need to be grouped. I've tried using a .pivot_table() like this. But it throws KeyError: 'desc_bal' In [20]: pd.pivot_table(df, values=['desc_bal', 'values'], index='def_desc', columns='J_col') Question - how do I pivot this df into this shape? -
Designing and displaying properly formatted data on django.
I am trying to make a model for a Blog and trying to format it in my template. Currently I have a single field for the "Body" of the blog, which will have all the content, but whenever I embed it in my template using {% for i in blog %} {{ i.body }} {% endfor %} Now if there was a link in the blog or an image or a piece of code I can't format it accordingly because all the data inside the Column body gets embedded as a big block of string. I have tried to change my model into having 10 different TextFields for both body and sub heading of the blog. I also have different TextFields for links and this is how the end model looks, can someone tell me whether this is the right way to do it or not. class Blog(models.Model): title = models.CharField(max_length=100, unique=True) slug = models.SlugField(max_length=100, unique=True) para1 = models.TextField(blank=True) subheading1 = models.CharField(max_length=200, blank=True) para2 = models.TextField(blank=True) subheading2 = models.CharField(max_length=200, blank=True) para3 = models.TextField(blank=True) subheading3 = models.CharField(max_length=200, blank=True) para4 = models.TextField(blank=True) subheading4 = models.CharField(max_length=200, blank=True) para5 = models.TextField(blank=True) posted = models.DateTimeField(db_index=True, auto_now_add=True) category = models.ForeignKey('blog.Category', on_delete=models.CASCADE) codeblock = models.ForeignKey('blog.Code', on_delete=models.CASCADE, null=True) … -
Resources blocked by robots.txt
I've set my website's robots.txt like this in order to allow some bots and block all others. User-agent: * Disallow: / User-agent: Googlebot User-agent: Googlebot-news User-agent: google User-agent: bingbot User-agent: bing User-agent: Slurp User-agent: slurp Allow: / Allow: .js Allow: .css Disallow: But when I check the website in varvy.com I still get an error saying: Resources blocked by robots.txt 33 CSS or javascript files are blocked Am I missing something? My website is powered by django and nginx. -
I am not getting data in proper language of html by request package
I am trying to scrape a Japanese website (a trimmed down sample below): <html> <head> <meta charset="euc-jp"> </head> <body> <h3>不審者の出没</h3> </body> </html> I am trying to get data of this html by request package using: response = requests.get(url) I am getting data from h3 field in as: '¡ÊÂçʬ' and unicode value of it is like this: '\xa4\xaa\xa4\xaa\xa4\xa4\xa4\xbf\' but when I load this html from a file or from a local wsgi server (tried with Django to serve a static html page) then I get: 不審者の出没. It's actual data. Now I am not understanding how to resolve this issue? -
Image display from database in django
The image in the path stored in database is different and when I access it and display, the image is different. Context - I'm building a CMS for images in Django and when an image is uploaded, I'm generating an Id for it and embedding that Id in the image using Pillow library package. Code is here. Dynamic text embedding in django For example: In this image, the text is embedded and it's located in /media/images/ But when I display the photo in my application, there is no text embedded though the same image from the same path is retrieved when I query the database. Here only the black and grey check jeans doesn't have the text whereas all the other images have it. What might be the reason for this? I'm guessing it has something to do with image size? -
How do I use curl to query graphene-django?
I tried this $ curl \ > -X POST \ > -H "Content-Type: application/json" \ > --data '{ "query": "{ posts { title } }" }' \ > http://localhost:8000/graphql But I got this response ... <h1>Forbidden <span>(403)</span></h1> <p>CSRF verification failed. Request aborted.</p> <p>You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p> <p>If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for &#39;same-origin&#39; requests.</p> ... I could make the graphql url exempt from CSRF token validation but that doesn't seem the right approach to me... -
python3 opencv is not saving readable video
I just want make the video to be web browser compatible. From the video objects I'm getting the image path, the image is png type I just want to convert those images to video and show it in the browser. path = settings.WEBUI_ROOT + '/events/' if not os.path.exists(path): os.makedirs(path) video = Video.objects.get(id=video_id) movements = Image.objects.filter(video=video) frame = cv2.imread(movements[0].file_path, 1) height, width, channel = frame.shape video_name = 'eventvideo.mp4' output_video = cv2.VideoWriter(path + video_name, cv2.VideoWriter_fourcc(*'MP4V'), 15, (height, width)) for _, image in enumerate(movements): image = Image.objects.get(id=image.id) frame = cv2.imread(image.file_path) output_video.write(frame) output_video.release() result = { 'success': True, 'path': "events/" + event_video_name } return JsonResponse(result, safe=False) Tools used Django and JS -
Custom __eq__() function for Django field
I need to have a CharField with custom __eq__ function so that when I run model_class.objects.filter(id=something) instead of just checking equality of both strings, my custom __eq__ function should be run. Or is there any other way to implement the same equality check other than creating a custom __eq__ function. -
Why is my template not getting view data from one func?
I have created a simple app for file upload. Everything worked fine.Then I wanted to add simple login and now I have problems. These are my views def login(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): cd = form.cleaned_data user = authenticate(username=cd['username'],password=cd['password']) if user is not None: if user is active: login(request,user) return HttpResponse('Authenticated successfully') else: return HttpResponse('Disabled account') else: return HttpResponse('Invalid login') else: form=LoginForm() return render(request,'account/login.html',{'form': form}) def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page documents = Document.objects.all() # Render list page with the documents and the form return render(request,'list.html',{'documents': documents, 'form': form}) This is fileupload/urls from django.conf.urls import url from fileupload.views import list from fileupload.views import login urlpatterns = [ url(r'^list/$', list, name='list'), url(r'^login/$', login, name='login'), ] My forms class LoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput) class DocumentForm(forms.Form): docfile = forms.FileField(label='Select a file') When I go to http://127.0.0.1:8000/fileupload/list/ everything works fine. If I try http://127.0.0.1:8000/login/ I got this I am confused becuase template is getting data from … -
Django: How to edit multiple rows in a model via forms
I have a model, Book, that has a ForeignKey on the model Library. There can be multiple books in a library. Since the information about each book is brief, (author, age, cost), I want to list out all of the books in the library on one form and have the user be able to update all 3 fields at once. There is a date field and a library_id field that determine which books were added to the library on each day. We will assume that no new books can be added in the current form. class Library(models.Model): user ... library_id = models.CharField(max_length=120, blank=True) updated = models.DateTimeField(auto_now=True) class Book(models.Model): library_id = models.ForeignKey(Library) date = models.DateField(default=None, blank=True, null=True) author = models.CharField(max_length=50, blank=True) age = models.IntegerField(default=0, min=0) cost = models.DecimalField(default=0.00, max_digits=100, decimal_places=2) updated = models.DateTimeField(auto_now=True) class Meta: unique_together = (('library_id ', 'date'),) I want to be able to select a date for a specific library and to be able to update all of the information on all of the books at once. This means in the back end, I'll be updating multiple records in the Book class. I see that this exists: inlineformset_factory, but when I try to use it (see below) I … -
AWS EC2 instance as server
What is a good EC2 instance to create if I want to configure a server by uwsgi and nginx with django 2.0 and python 3.x? I want to know because I need procesing power for file procesing, matrix operations and the files can be thousands of rows long and the matrix operations are in the [1000, 7][7, 1000, ecuaions up to grade 7(8+ tend to break python), chart generation, and i want it to be as fast as posible without code optimization. I tried the free small.t2 instance and the large.x4 or something and i cant feel the diference, i think a greaphic instance would be the best but then again, this is pure speculation. -
Why can't I get the followed users of the current user in my Django application?
I am using the standard auth.User model for the User objects, and my Follow object model is defined as follows: class Follow(models.Model): owner = models.ForeignKey( 'auth.User', related_name='followers', on_delete=models.CASCADE, null=False ) following = models.ForeignKey( 'auth.User', related_name='following', on_delete=models.CASCADE, null=False ) The serializer I am using is as follows: class PublicUserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', 'username') read_only_fields = ('id', 'username') And my view is as follows: class FollowingView(generics.ListAPIView): serializer_class = PublicUserSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): return self.request.user.following.all() This is returning an empty result set, for some reason that I cannot understand. However, if I use the following code for the view, it DOES return the correct queryset: class FollowingView(generics.ListAPIView): serializer_class = PublicUserSerializer permission_classes = (permissions.IsAuthenticated,) def get_queryset(self): follows = Follow.objects.filter(owner=self.request.user).values_list('following_id', flat=True) return User.objects.filter(id__in=follows) So why can't I get the correct queryset by using self.request.user.following.all()? -
django queryset F get last
django 2.0.2 python 3.4 models.py Post(models.Model): Id = pk content = text Reply(models.Model): Id = pk PostId = Fk(Post) content = text view.py Post.objects.all().annotate(lastreply=F("Reply__content__last")) can use last query in F() ? -
Unable to install django-redactorjs due to setup.py egg_info error code 1
I've been trying to install django-redactorjs using pip but it returns this: Collecting django-redactorjs Using cached https://files.pythonhosted.org/packages/9b/52/016fda62f70f6b0c7be462ccb5e0fe2b9da1960fdbdd708c650cb4a974b8/django-redactorjs-0.3.5.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\yumie\AppData\Local\Temp\pip-install-8yx4o35h\django-redactorjs\setup.py", line 6, in <module> readme = f.read() File "c:\users\yumie\envs\ojt\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1639: character maps to <undefined> ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in C:\Users\yumie\AppData\Local\Temp\pip-install-8yx4o35h\django-redactorjs\ I've already tried updating pip but it's still the same error. -
Django formset for models without foreign key
I'm trying to use formset to create connections between my Neo4j nodes. These models have been constructed using the django-neomodel package. They aren't related using foreign keys, but since this isn't an inline formset that shouldn't matter, right? models.py class Person(DjangoNode): uid = UniqueIdProperty() name = StringProperty(max_length=50) created_at = DateTimeProperty(default=datetime.now) friends = RelationshipTo('Friend', 'FRIENDED', model=FriendRel) # DjangoNode class must have a Meta class and documentation specifies 'django_node' class Meta: app_label = 'django_node' class FriendRel(DjangoRel): created_at = DateTimeProperty(default=datetime.now) class Meta: app_label = 'django_rel' forms.py class PersonForm(forms.ModelForm): class Meta: model = Person # don't need fields for automatically assigned keys like `uid` and `created_at` fields = ['name'] class FriendRelForm(forms.ModelForm): class Meta: model = FriendRel exclude = () #"creating ModelForm without either 'fields' || 'exclude' attribute is prohibited" FriendRelFormSet = formset_factory(FriendRelForm, extra=1) form.html <div> <form action="" method="post">{% csrf_token %} {{ form.as_p }} <table class="table"> {{ friends.management_form }} {% for form in friends.forms %} {% if forloop.first %} <thead> <tr> {% for field in form.visible_fields %} <th>{{ field.label|capfirst }}</th> {% endfor %} </tr> </thead> {% endif %} <tr class="{% cycle "row1" "row2" %} formset_row"> {% for field in form.visible_fields %} <td> <!-- Include the hidden fields in the form --> {% if forloop.first %} … -
Django: unique id generator
I have a pres_save feature that creates a unique ID for a field in a model. I am having trouble checking to see if the field is unique: def pre_save_create_invoice_id(sender, instance, *args, **kwargs): if not instance.invoice_id: instance.invoice_id = unique_id_generator('invoice_id',instance) pre_save.connect(pre_save_create_invoice_id, sender=Invoice) def unique_id_generator(field,instance): new_id = random_string_generator() Klass = instance.__class__ qs_exists = Klass.objects.filter(field=new_id).exists() if qs_exists: return unique_slug_generator(instance) return new_id When I run the code, I get the error that 'Cannot resolve keyword 'field' into field. Choices are: ... invoice_id...' Basically it errors out on this line: qs_exists = Klass.objects.filter(field=new_id).exists() saying that 'field' is not a field in the model. Why is it using the word 'field' instead of the value for field e.g. invoice_id? -
service worker does my web slower! , how to add offline capabilities to Django work using workbox
I have been trying since some days to give offline capabilities using workbox to my Django Web App without success. I have follow the get started guide and I manage to register with succeed the service worker and to save/serve from cache the the static and media resources. Code done to archive the describe: urls.py ... url(r'^service-worker.js', cache_control(max_age=60*60*24)(TemplateView.as_view( template_name="sw.js", content_type='application/javascript', )), name='sw.js'), ... base.html template ... <!-- bottom of body --> <script> // Check that service workers are registered if ('serviceWorker' in navigator) { // Use the window load event to keep the page load performant window.addEventListener('load', () => { navigator.serviceWorker.register('{% url 'sw.js' %}'); }); } </script> ... sw.js (Service Worker) importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js'); if (workbox) { console.log(`Yay! Workbox is loaded 🎉`); } else { console.log(`Boo! Workbox didn't load 😬`); } workbox.setConfig({ debug: false }); // workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug); workbox.routing.registerRoute( /\.(?:js|css)$/, workbox.strategies.staleWhileRevalidate({ cacheName: 'static-resources', }), ); workbox.routing.registerRoute( /\.(?:png|gif|jpg|jpeg|svg)$/, workbox.strategies.cacheFirst({ cacheName: 'images', plugins: [ new workbox.expiration.Plugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days }), ], }), ); workbox.routing.registerRoute( new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'), workbox.strategies.cacheFirst({ cacheName: 'googleapis', plugins: [ new workbox.expiration.Plugin({ maxEntries: 30, }), ], }), ); After this , I decide to check the performance, and see if service workers were helping … -
How to get ROOT folders url inside an APP in Django
I'm new to Django and i can't understand this basic stuff. In an HTML file a have this string: <form action=media/ method="POST" encrypt="multipart/form-data"> and i want to get the url: media/ but using the code above i get Not Found: /API/MEDIA_ROOT In the url appears always API before. How can i go down to the root and have only /media instead of /API/media? my url.py from django.urls import path from django.conf.urls import url from . import views app_name = 'API' urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^order', views.order, name='order'), url(r'^get_order', views.get_order, name='get_order'), url(r'^send_news', views.send_news, name='send_news'), url(r'^get_news', views.get_news, name='get_news'), url(r'^invia_news', views.invia_news, name='invia_news'), ] and: urlpatterns = [ path('API/', include('API.urls',namespace="API")), path('admin/', admin.site.urls), path('', index, name='index'), path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}) ] urlpatterns += [ path('accounts/', include('django.contrib.auth.urls')), ] -
'Questions' object has no attribute 'choice_set'
I've been following the Django documentation "Write your app tutorial" and I keep running into the above error. It seems to be coming from this line selected_choice = question.choice_set.get(pk=request.POST['choice']) This is my Questions object: class Questions(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date Published') def __str__(self): return self.question_text The code is exactly as it is on the official documentation, so I can't tell exactly where the error is coming from -
Django jwt middleware for channels websocket authentication
I'm trying to set a Authentication middleware for django channels. I want this middleware to be active only for websocket requests. Seems like that in this case i don't get a full middleware functionality. For example i can't get response = self.get_response(scope) working: 'TokenAuthMiddleware' object has no attribute 'get_response' Everything is allright with this middleware now (it is activated only for websocket requests and not registered in settings.py), except that i need a means to modify a response status codes (block anonymous users and set the error code for ExpiredSignatureError). Any help appreciated. I use Django 2.0.6 and channels 2.1.1. jwt authentication by djangorestframework-jwt middleware: import jwt, re import traceback import logging from channels.auth import AuthMiddlewareStack from django.contrib.auth.models import AnonymousUser from django.conf import LazySettings from jwt import InvalidSignatureError, ExpiredSignatureError, DecodeError from project.models import MyUser settings = LazySettings() logger = logging.getLogger(__name__) class TokenAuthMiddleware: """ Token authorization middleware for Django Channels 2 """ def __init__(self, inner): self.inner = inner def __call__(self, scope): headers = dict(scope['headers']) auth_header = None if b'authorization' in headers: auth_header = headers[b'authorization'].decode() else: try: auth_header = _str_to_dict(headers[b'cookie'].decode())['X-Authorization'] except: pass logger.info(auth_header) if auth_header: try: user_jwt = jwt.decode( auth_header, settings.SECRET_KEY, ) scope['user'] = MyUser.objects.get( id=user_jwt['user_id'] ) except (InvalidSignatureError, KeyError, ExpiredSignatureError, DecodeError): … -
How to sort array in "chess order"?
The task is to sort queryset in "chess order". ie: class Item(models.Model): CHOICES = [ (1, 1), (2, 2), (3, 3), ] branch = models.PositiveSmallIntegerField(choices=CHOICES) item1.branch == 1 item2.branch == 1 item3.branch == 2 item4.branch == 3 item5.branch == 3 The desired output of Item.objects.all() would be: [item1, item3, item4, item2, item5] So the resulted queryset would be sorted in a manner where branches are (1,2,3), (1,2,3), (1,2,3) etc. -
android format datetime from django
I'm connecting my django website to an android app using json and the rest framework The json data contains datetime like this : { date: "2018-06-05T12:42:48.545140Z" } When android receives the date, I try to format it using this code : String dt="2018-06-05T12:42:48.545140Z"; DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm"); String date=formatter.format(Date.parse(dt)); And I get the following error : java.lang.IllegalArgumentException: Parse error: 2018-06-14T14:30:02.982009Z at java.util.Date.parseError(Date.java:367) at java.util.Date.parse(Date.java:448) In a django a template I can easily do this {{article.date|date:'d-m-Y H:i'}} But in android I'm a bit confused -
How to set already selected value (Update form) in dynamic dropdown Django template
In django template I am showing dropdown like this . My all dropdown attribute names are in variable event_dropdown. And I am assigning like this code <select name="event_value" > <option value="">---------</option> {% for event in event_dropdown %} <option value="{{ event.id }}">{{ event.name }}</option> {% endfor %} </select> Now Issue is In Update case How I can show my already selected value here ? As All things are in for loop -
Django stackedinline showing unrelated fields only
I have a location based app: Country -> County: class State(models.Model): class City(models.Model): state=models.ForeignKey(State) class County(models.Model): city=models.ForeignKey(city) I need to collect the climate related information about each, which I already have. Instead of creating three separate models, I decided to use on model: class PlaceWeather(models.Model): state=models.ForeignKey(State,db_index=True,null=True,default=None) city=models.ForeignKey(State,db_index=True,null=True,default=None) county=models.ForeignKey(State,db_index=True,null=True,default=None) start_month=models.PostiveIntegerField() end_month=models.PositiveIntegerField() then climate oriented data that was collected over the past 3 years. Now my issue is on the admin area. class PlaceWeatherInline(admin.StackedInline): model = PlaceWeather can_delete = False verbose_name_plural = _('PlaceWeather') class StateAdmin(admin.ModelAdmin): list_display = ('name', ) inlines=(PlaceWeatherInline,) Here, in StateAdmin section, it shows City and County weather fields only. In CityAdmin, it omits City and shows State and County and in CountyAdmin, it omits county and shows State and City in the stacked inline section. My intention is to display state field of PlaceWeather in StateAdmin only or at least display it along wit City and County. How do I do achieve that?