Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
how to make my query fast in django project?
in my projects,i have these models; ``` class Invoice(models.Model): #this must named to payorder account = models.ForeignKey('account.Account') # 购买的人 product = models.CharField('支付产品', choices=PAY_PRODUCTS,max_length=30) fee = models.PositiveIntegerField('金额',default=0) ip = models.CharField('IP地址', max_length=30) paid = models.BooleanField('已支付', default=False, db_index=True) refund_status = models.CharField('退款状态', max_length=10, choices=REFUND_STATUS, default='unrefund') refund_reason = models.CharField('退款原因', max_length=1024, default='') transaction_id = models.CharField('支付ID', max_length=30, null=True) create_time = models.DateTimeField('创建时间', auto_now_add=True) pay_time = models.DateTimeField('支付时间', null=True, db_index=True) object_id = models.PositiveIntegerField('支付对象',db_index=True, default=0, blank=True) object_option_id = models.PositiveIntegerField('支付对象备注', default=0, blank=True, db_index=True) source = models.CharField('支付来源', max_length=10, choices=PAY_SOURCE_TYPE, default='jsapi', blank=True) seller = models.ForeignKey('account.Account', null=True, default=None, blank=True, related_name='sells') comment = models.CharField('备注', max_length=1024, default='') ``` i need to select this data from create_time with all ForeignKey. -
Launch existing project Django
I'm new to Django. I set up a Django project in a virtual environment a few days ago and worked on the project. Now I'm again trying to work on the project but when I try python manage.py runserver, it just runs a new "Welcome to Django" page and not the old project I was working in. How do I launch my existing virtualenv Django project rather than starting a new one? -
Django-taggit. Multi-tags in a model. Retrieve data
Here is a model with multiple tags in it. How do I retrieve data from tags_en? tags.names() works well but not tags_en.names() nor tags_en.all() from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase, TagBase from taggit_selectize.managers import TaggableManager class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase): class Meta: verbose_name = _("Tag") verbose_name_plural = _("Tags") class BaseTag (TagBase): pass class UUIDTaggedItemEn (GenericUUIDTaggedItemBase, TaggableManager): tag = models.ForeignKey(BaseTag) class Item(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) tags = TaggableManager(blank=True, through=UUIDTaggedItem) tags_en = TaggableManager(blank=True, through=UUIDTaggedItemEn) Error message is: FieldError at /admin/item/item/ Cannot resolve keyword 'None' into field. Choices are: category, id, item, name, slug, taggroup, uuidtaggeditemen -
HTML user input validation for bash script
I'm working on a website (Python/Django) where at some point, the user may write a short text that will then be processed as an argument in a bash script. (I.e. something like this: echo "USERINPUT".) Is it safe enough to check the input (both client- and server-side) using this regex: ^[a-zA-ZÀ-ÿ0-9_ !?:;'=@\/)(°#*%&\-\n]{1,500}$ It basically allows alphanumeric characters, accented letters (À-ÿ) and a limited number of special characters excluding \ and ". Is this reasonably safe? -
Looping over response to save items to the DB (Django,Scrapy)
I'm on a project where i crawl the web for various furnitures and get their attributes. I used DjangoItem to save these data to my DB. My problem is getting the response count to use it in a for loop. I don't know if it's the best way to do it(probably not). Here's my spider: def parse(self, response): now = timezone.now() for item in response.css('.page-content-area'): for i in range(5): furnitureItem = FurnitureItem() furnitureItem['furniture_type'] = 'Chair' furnitureItem['name'] = item.css('h2 a::text').extract()[i] furnitureItem['description'] = item.css('a span::text').extract()[i] furnitureItem['href'] = item.css('h2 a::attr(href)').extract()[i] furnitureItem['img_source'] = item.css('.lazy::attr(data-original)').extract()[i] furnitureItem['price'] = item.css('.mt3::text').extract()[i] furnitureItem['last_update'] = now yield furnitureItem Each key here has multiple values. I looped 5 times to see if it saves 5 items to the DB. It does. What i want is to get response's count to use it in this loop. I just started to use Scrapy and i feel like i can't use it effectively. I'll appreciate any advice. And an extra question: What does a pipeline do and in which cases should i use it. I've read official documents but i feel like i need a simple explanation. Thank you. -
Wagtail Admin Images Crash - How to get more crash information?
Since I updated my production setup to Wagtail 1.11 I cannot load the admin page for images. Visiting /admin/images/ results in a 502 error. In my development setup I don't have the same problem That this is the result of a crash of the runner is supported by the logs: nginx: 2017/07/11 08:28:49 [error] 1726#1726: *25 upstream prematurely closed connection while reading response header from upstream, client: 162.158.222.110, server: XXXXX,, request: "GET /admin/images/ HTTP/1.1", upstream: "http://unix:/run/XXXXX/socket:/admin/images/", host: "XXXXX", referrer: "https://XXXXX/admin/images/" unicorn: Jul 11 08:28:49 Moore gunicorn[1559]: [2017-07-11 08:28:49 +0000] [2083] [INFO] Booting worker with pid: 2083 Jul 11 08:28:51 Moore gunicorn[1559]: DEBUG 2017-07-11 10:28:51,017 base 2083 139986153277184 Configuring Raven for host: <raven.conf.remote.RemoteConfig object at 0x7f510787f518> However I don't receive any reason as to why the runner crashed. Sentry, which receives all warnings and up, doesn't report any warnings being received. Does anyone know what problem I'm facing and how to solve it? Or at least a way for me to receive more information? P.S. Updating to Wagtail 1.11.1 did not solve my problem. -
DRF SearchFilter fixed field value
Hi there i would like to ask regarding the searchfilter class in this example. class UserListView(generics.ListAPIView): queryset = User.objects.all() serializer_class = UserSerializer filter_backends = (filters.SearchFilter,) search_fields = ('username', 'email') This will allow the client to filter the items in the list by making queries such as: http://example.com/api/users?search=russell Now what i want to do is, for example add a gender field to User object and search filter it returning all male in the queryset. It would be something like this search_fields = ('username', 'email', 'gender="male"') # of course this wont work. How can we attain this thing in searchFilter? -
Could not import 'oauth2_provider.ext.rest_framework.OAuth2Authentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'
It's the first time I work with django rest and Django Oauth toolkit I'm following this tutorial oauth2-with-django-rest-framework But when I run python manage.py migrate I get the following error: ImportError: Could not import 'oauth2_provider.ext.rest_framework.OAuth2Authentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: No module named ext.rest_framework. What is going wrong ? is there another module I should install ? my virtual environment contains : certifi==2017.4.17 chardet==3.0.4 Django==1.11.2 django-extensions==1.8.1 django-oauth-toolkit==1.0.0 djangorestframework==3.6.3 idna==2.5 oauthlib==2.0.2 pytz==2017.2 requests==2.18.1 six==1.10.0 Unidecode==0.4.21 urllib3==1.21.1 -
How to dynamically alter field mapping in django restframework's serializer that uses nested serializers?
I use the following serializer in most requests such as GET, POST etc: class PrescriptionSerializer(serializer.ModelSerializer): tags = TagSerializer() setting = SettingSerializer() But, I want to map setting field to SettingUpdateSerializer() if request.action is UPDATE(=PUT/PATCH). Without diving PrescriptionGetSerializer and PrescriptionUpdateSerialzer and using them accordingly, is there a way to dynamically map serializer-nesting field to other serializer, as below? class PrescriptionSerializer(serializer.ModelSerializer): tags = TagSerializer() setting = SettingUpdateSerializer() I though about using self.fields.pop on __init__, but this way it is only possible by using different different field names such as update_setting and get_setting. Thanks for the help in advance. -
django-import-export: cannot exclude id field during import : KeyError: u'id'
On Django-1.9.6, django-import-export-0.5 When I try to upload CSV without "id" field throws this error. Line number: 1 - u'id' 13173474, Harry McDade, 10.harry.asas@asasasas.com Traceback (most recent call last): File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 434, in import_row instance, new = self.get_or_init_instance(instance_loader, row) File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 258, in get_or_init_instance instance = self.get_instance(instance_loader, row) File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/resources.py", line 252, in get_instance return instance_loader.get_instance(row) File "/Users/isanka/dev/venv/edxubase/lib/python2.7/site-packages/import_export/instance_loaders.py", line 31, in get_instance field = self.resource.fields[key] KeyError: u'id' -
django allauth: redirecting for invalid passwords
I have a custom page which calls allauth signup view (custom_passcheck.html) <form action="{% url 'account_signup' %}" method="post" class="form"> {% csrf_token %} <BR> <BR> <BR> <center> <div class=signuponepage> <div class=sign-up> <div class=signup-box> <div class="form-element email-address"> <label id="email-address-label"> <strong> Create a password (it must be 6 characters long) </strong> <input type="hidden" name="username" value="{{ pemail }}"> <input type="hidden" name="email" value="{{ email }}"> <input type="text" maxlength="19" name="password1" value="" placeholder="Password"> <input type="text" maxlength="19" name="password2" value="" placeholder="Password (again)"> </label> </div> <div class="form-element nextstep-button"> {% buttons %} <button type="submit" class="btn btn-primary btn-sm"> Finish {% bootstrap_icon "chevron-right" %} </button> {% endbuttons %} </div> </div> </div> </div> </center> </form> Now, if a user puts in password less than 6 chars, it will raise an error and redirect the user to allauth signup page (/account/signup) Question: instead of redirecting to (/account/signup), how can i redirect to the custom page (custom_passcheck.html) while preserving POST data -
No module named constants error
I have run my project with "python manage.py run server" and it runs truthfully. but when I debug it, it said "from django.db.models.constants import LOOKUP_SEP No module named constants". what should I do? django version==1.4 python==2.7.12 -
JQueryEditor error python
I'm getting the following error from attempting to run my django app on salesforce cloud9. It is giving me a JQueryEditor, error even though I've installed django-redactor. Anything else I should install? Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 324, in execute django.setup() File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate app_config.ready() File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready self.module.autodiscover() File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 26, in autodiscover autodiscover_modules('admin', register_to=site) File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 50, in autodiscover_modules import_module('%s.%s' % (app_config.name, module_to_search)) File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 2254, in _gcd_import File "<frozen importlib._bootstrap>", line 2237, in _find_and_load File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked File "<frozen importlib._bootstrap>", line 1129, in _exec File "<frozen importlib._bootstrap>", line 1471, in exec_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed File "/usr/local/lib/python2.7/dist-packages/redactor/admin.py", line 3, in <module> from redactor.widgets import JQueryEditor ImportError: cannot import name 'JQueryEditor' -
Humhub authentication in django
I need to authenticate my users with humhub, need to register humhub account for new users and login module for existing users. Have any one tried humhub in any django app. Thanks in advance -
trying querying from db using datatable,django-- DataTables warning: table id=DataTables_Table_0 - Ajax error. please see http://datatables.net/tn/7
I'm trying the datatable in my django app ,and this in my code in my view file ,i'm getting the error DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7 reachingtime.py (view file) class OrderListJson(BaseDatatableView): model = TblUser columns = ['fname','lname'] order_columns = ['fname','lname'] max_display_length = 500 def __init__(self): pass def render_column(self, row, column): # We want to render user as a custom column if column == 'user': return '{0} {1}'.format(row.fname, row.lname) else: return super(OrderListJson, self).render_column(row, column) def get_initial_queryset(self): return TblUser.objects.filter(is_active=1) def filter_queryset(self, qs): search = self.request.GET.get(u'search[value]', None) #from_date = FORMATTED_TIME_PLUS = datetime.datetime.strptime(str(from_date), '%Y-%m-%d') if search: qs = qs.filter(name__istartswith=search) return qs def prepare_results(self, qs): json_data = [] for item in qs: print item json_data.append([ item.fname, item.lname, ]) return json_data template file (index.html) $(document).ready(function() { var oTable = $('.datatable').dataTable({ // ... "processing": true, "serverSide": true, "ajax": "datatable/data/" }); // ... }); -
Delaying iterations within loop of a scheduled celery task
In a Django web app I maintain, I have a certain scheduled celery task that loops over a list of phone numbers and emits text messages to them (I use the Twilio API for this). I don't have a short code assigned, and don't want to break rate limits instituted by telcos (~1 msg/s). What would be the best way to spread out bulk processing of this SMS list, in order to avoid rate limits? One thing from the top of my head is to put in time.sleep(2) in the loop processing the text messages. But I've never used a delay function like that in celery tasks before, and nor in production, so I'm apprehensive of undesirable by-products of doing this. Can someone shed more light on the best way to handle this? -
django tastypie rest permissions
I'm looking to make changes to django web app that provides a REST interface via tastypie. The app is available at: https://github.com/OWASP/django-DefectDojo/ Within the app, Users have Products that they are authorised to view and Endpoints belonging to the Products. The models are defined at: https://github.com/OWASP/django-DefectDojo/blob/master/dojo/models.py#L177 https://github.com/OWASP/django-DefectDojo/blob/master/dojo/models.py#L417 I have added EndpointResource to dojo/api.py: class EndpointResource(BaseModelResource): class Meta: queryset = Endpoint.objects.all() resource_name = 'endpoints' fields = ['product', 'protocol', 'host', 'path', 'query', 'fragment'] list_allowed_methods = ['get'] detail_allowed_methods = ['get'] include_resource_uri = True filtering = { 'product': ALL, 'protocol': ALL, 'host': ALL, } authorization = DjangoAuthorization() authentication = DojoApiKeyAuthentication() serializer = Serializer(formats=['json']) class Product contains: authorized_users = models.ManyToManyField(User, blank=True) class Endpoint contains: product = models.ForeignKey(Product, null=True, blank=True, ) Currently, Users can authenticate to /api/v1/endpoints/ and they will see all Endpoints. curl -v --header 'Authorization: ApiKey sue:5b632d76ef1a38b8375383e3498d063515b356d4' http://example.com/api/v1/endpoints/ However, the desired behaviour is that Users should only be able to access Products they are authorised for, along with related entities to these products. From within a python session I can do: >>> from dojo.models import User, Product, Endpoint >>> User.objects.get(username='sue').product_set.all().get().endpoint_set.all() [<Endpoint: goliath.sue.local>, <Endpoint: goliath.suelimited.co>, <Endpoint: 192.168.10.11>] These objects, associated with 'sue' are the ones that I want returned by the API. What would be … -
Keep option selected on page refresh
So how my code works i'll give you a gist. When there are no files in html_files, the default option is "---", but when there exists a file in html_files there are two options now, 1) "---" 2) file. But with default still as "---" So what i want to do is, when there exists a file in html_files I want the default option change to the current file and not "---". I cant think of a way as to how to do it. Could someone help me? <span title="list resources who's involved etc">About File: <select class="experiment_file_selector" id="about_file" name="about_file"> <option value="None" {% if not exp.about_file %}selected="selected"{% endif %}>---</option> {% for file in html_files %} <option value="{{ file.id }}" {% if file == exp.about_file %}selected="selected"{% endif %} >{{ file.get_base_name }}</option> {% endfor %} </select></span> -
django form not updating as expected
Here is my model: class Browser(models.Model): profile_name = models.CharField(max_length=400) browser_type = ( ('fr', 'Firefox'), ('ch', 'Chrome'), ('op', 'Opera'), ('ot', 'Other'), ) browser_name = models.CharField(choices=browser_type, max_length=2) device_name = models.CharField(max_length=400) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Bookmark(models.Model): browser = models.ForeignKey(Browser, on_delete=models.CASCADE, null=True, blank=True) title = models.TextField() url = models.TextField() iv = models.TextField() salt = models.TextField() iteration = models.IntegerField(default=1500) tags = TaggableManager() I only want to update certain fields, so here is the modelform class BookmarkFormEdit(ModelForm): class Meta: model = Browser exclude = ('tags', 'browser_name', 'device_name', 'profile_name') but my problem is, values are not updating as expected . Here is the view: def bookmark_edit(request, pk=None): if request.method == 'POST': bookmark = Bookmark.objects.get(pk=pk) frm = BookmarkFormEdit(request.POST, instance=bookmark) print(request.POST.get('iteration')) // printing correct value from froend if frm.is_valid(): x = frm.save() print(x.iteration) // not saving the new value ! return JsonResponse({'status': 'created'}) else: return JsonResponse({'error': frm.errors}) return render(request, 'bookmark_edit.html', {'pk': pk}) -
difficulty to add image in recommendation system
I tried to add image in the reviews page of my project.The data is loaded but the image is not updated in the page.It just shows a blank white box and nothing else.Could you please help to find the error. my models.py looks like this: class Review(models.Model): RATING_CHOICES = ( (1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), ) wine = models.ForeignKey(Wine) pub_date = models.DateTimeField('date published', null=True) user_name = models.CharField(max_length=100) comment = models.CharField(max_length=200) rating = models.IntegerField(choices=RATING_CHOICES) images = models.ImageField(null = True, blank=True) the code for reviews_list look like this: {% extends 'base.html' %} {% block title %} <h2>Latest reviews</h2> {% load static %} {% endblock %} {% block content %} {% if latest_review_list %} <div class="row"> {% for review in latest_review_list %} <div class="col-xs-6 col-lg-4"> <h4><a href="{% url 'reviews:review_detail' review.id %}"> {{ review.wine.name }} </a></h4> <br> <a><img src="{% static wine.images.url %}" height="200"></a> <h6>rated {{ review.rating }} of 5 by <a href="{% url 'reviews:user_review_list' review.user_name %}" >{{ review.user_name }}</a></h6> <p>{{ review.comment }}</p> </div> {% endfor %} </div> {% else %} <p>No reviews are available.</p> {% endif %} {% endblock %} the code for views.py look like this: from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect from django.core.urlresolvers import … -
ForeignKey that can point to 1 of 2 possible models
What I want is Model A to have an instance variable (ForeignKey) that points to either Model B or Model C. To represent it in code: class Exercise(models.Model): f_key = models.ForeignKey(ModelB or ModelC) Obviously the synax is invalid here because of the 'or' but it illustrates what I want to do. I've read into GenericForeignKey but it seems overly complex for what I want to do. Can I do this without the use of GenericForeignKey, in favor of something simpler? -
Django Projects and git
So I am curious how others handle the security of version control on github or other public domain sites, of web frameworks like django. The settings.py can and will often contain sensitive database information. What is the common practice and least hassle way of handling that? -
Passing JSON strings to Django templates
I've been banging my head against the wall trying to figure out why I can't pass a JSON string generated from a Django Model into a template's javascript static file. As it turns out, the problem wasn't at the Model level (using serializers.serialize) - putting an identical string in the script itself will successfully parse, but passing the string in will not: views.py: def texas(request): test_json = '{"incidents": [{"field1": 1, "field2": 2}], "debug": 1}' return render(request, 'tx/texas.html', {'tx': test_json}) tx/texas.html: {% load staticfiles %} <html> <head> <title>texas map</title> </head> <body> <p id='texas'></p> <script> function load_texas() { return '{{ tx }}'; } </script> <script src="{% static 'js/texas.js' %}"></script> </body> </html> js/texas.js fails with JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data: var json_data = load_texas(); var paragraph = document.getElementById('texas'); try { paragraph.innerHTML = JSON.parse(json_data); } catch(err) { paragraph.innerHTML = err.message; } but is successful if the same string is just entered in the script: // this JSON string is identical to the one passed in views.py var json_data = '{"incidents": [{"field1": 1, "field2": 2}], "debug": 1}'; Am I missing something about the way Django handles context variables? -
Django js variable issue
How I can solve my problem? <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> </head> <body> <div id="demo"> <p>{% verbatim %}{{message}}{% endverbatim %}</p> </div> <script> var demo = new Vue({ el: '#demo', data: { message: 'Hello Vue.js!' } }); </script> </body> </html> Works without any problems, but: <html> <head> <meta charset="utf-8"> {% load static %} <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script> <script type="text/javascript" src="{% static 'index/script.js'%}"></script> </head> <body> <div id="demo"> <p>{% verbatim %}{{message}}{% endverbatim %}</p> </div> </body> </html> Browser show "{{message}}". Can you help me? I have problem with .js file including. JS file: var demo = new Vue({ el: '#demo', data: { message: 'Hello Vue.js!' } }) -
Django Best Practices? image upload
Is it a good idea to use contenttypes in my project ?, I have a user model, post and event, all three have in common an image field. I just start with django and I prefer that this does not cause me problems later.