django-views

django - collecting data from a HTML <select>

不打扰是莪最后的温柔 提交于 2019-11-30 05:38:50
问题 I've seen a couple of documents on how to collect the data from a HTML statement in Django but none of them were very clear to me. Does anybody have a real working example to share? In my case I have something like this in my template file: <select title="my_options"> <option value="1">Select value 1</option> <option value="2">Select value 2</option> </select> What goes in the views.py in order to collect the selected value? Thank you! 回答1: If it's a GET request, request.GET['my_options'] .

Django per user view caching

十年热恋 提交于 2019-11-30 05:22:53
I need a per user caching. The regular view caching does unfortunately not support user-based caching. I tried the template fragment caching like this: {% load cache %} {% cache 500 "mythingy" request.user %} ... HTML stuff ... {% endcache %} but it's slow as hell. Does anybody know a faster way to achieve what I need? Thanks! ifedapo olarewaju as of django >=1.7, using the cache_page along with vary_on_cookie decorators on your view should solve this. something like this. from django.views.decorators.vary import vary_on_cookie from django.views.decorators.cache import cache_page @cache_page

django - django-taggit form

随声附和 提交于 2019-11-30 05:18:34
I would like to use django-taggit ( click here ). The documentation ( click here ) talks about using ModelForm to generate the form but I have already my form that I would like to use. Let's say if I have something like this: forms.py class MyForm(forms.Form): ...... tags = forms.CharField(max_length=200, widget=forms.Textarea) how do I save the the tags coming from the tags field? What goes in my views.py ? A real example would be truly appreciated. I'm not too familiar with the django taggit app, but it looks like if you want to use the same field and widget setup the app uses, you can

How to get information from Django_tables2 row?

风流意气都作罢 提交于 2019-11-30 05:12:39
问题 I have declared a table and want to fetch the row's value which is checked using checkboxfield. Any help, how can i write this event in my views so that everytime I select a row and hit submit button, it returns the row's values.Code goes like this: class mytables(tables.Table): new_database = tables.CheckBoxColumn() student =tables.Column(accessor='Student') Class = tables.Column(accessor='class') And in my templates a submit button. 回答1: You need to choose a suitable value for the

Django: 'module' object has no attribute 'index'

谁都会走 提交于 2019-11-30 05:02:49
问题 I've been trying to learn Django for the past few days, but recently I've stumbled upon a problem I can't seem to fix. After finishing Django's own tutorial on writing your first app I decided to go through it again. Only now I would replace everything to fit the requirements of the original app I was building. So, everything went well until I got to part 3. When I try to load http://localhost:8000/lru/ I get the following error message: AttributeError at /lru/ 'module' object has no

Django serve static index.html with view at '/' url

家住魔仙堡 提交于 2019-11-30 04:41:30
I have my index.html in /static/ folder. My django app is running ok when i try: http://127.0.0.1:8000/index.html But i want to acces index.html by url: http://127.0.0.1:8000/ I wrote a view and it works: class IndexView(TemplateView): template_name = 'index.html' I also added to urls.py(this lets me serve static like http://127.0.0.1:8000/css/style.css ): url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', { 'document_root': settings.STATIC_ROOT, 'show_indexes':True }), But i think there is a way to do what i want without TemplateView. Any suggestions? Thanks. My django version

How to return multiple objects related with ForeignKey in Django

大憨熊 提交于 2019-11-30 04:41:29
问题 I have the following in my models.py: class HostData(models.Model): Manager = models.ForeignKey(Managers) Host = models.CharField(max_length=50, null=True) HostStatus = models.CharField(max_length=200, null=True) Cpu = models.PositiveIntegerField(max_length=10, null=True) Disk = models.FloatField(null=True) I would like to return the query for objects related to a certain "Manager". The problem is that the user may add/delete as many managers as he wants. So my initial thought was to have in

How to get primary keys of objects created using django bulk_create

蓝咒 提交于 2019-11-30 04:36:17
Is there a way to get the primary keys of the items you have created using the bulk_create feature in django 1.4+? Or Duan 2016 Since Django 1.10 - it's now supported (on Postgres only) here is a link to the doc . >>> list_of_objects = Entry.objects.bulk_create([ ... Entry(headline="Django 2.0 Released"), ... Entry(headline="Django 2.1 Announced"), ... Entry(headline="Breaking: Django is awesome") ... ]) >>> list_of_objects[0].id 1 From the change log: Changed in Django 1.10: Support for setting primary keys on objects created using bulk_create() when using PostgreSQL was added According to

How can i pass data to django layouts (like 'base.html') without having to provide it through every view?

核能气质少年 提交于 2019-11-30 03:42:01
I am trying to pass the data to layout 'base.html' . I am currently doing it by storing the data in request.session and accessing it in 'base.html' through request object. Is there any way to pass the data to 'base.html' without having to pass the data from every views? Use a context processor, which is made exactly for that purpose. Create a file context_processors.py in one of your app directories, then in the file define a function that return a dictionary of variables to insert in every template context, something like this: def add_variable_to_context(request): return { 'testme': 'Hello

QuerySet, Object has no attribute id - Django

倾然丶 夕夏残阳落幕 提交于 2019-11-30 03:39:19
I'm trying to fetch the id of certain object in django but I keep getting the following error Exception Value: QuerySet; Object has no attribute id. my function in views.py @csrf_exempt def check_question_answered(request): userID = request.POST['userID'] markerID = request.POST['markerID'] title=request.POST['question'] m = Marker.objects.get(id=markerID) u = App_User.objects.get(id=userID) print userID print markerID print title # userID='1' # markerID='1' # title='Hello' at = AttachedInfo.objects.filter(attachedMarker=m.id, title=title) print 'user' print u.id print 'marker' print m.id