django-views

How to get hostname or IP in settings.py so that i can use it to decide which app's urls to use

旧时模样 提交于 2019-12-06 16:13:03
I am making a django application where it will have 2 apps. When you open www.webName.co.id it will use urls.py from app A, but when you open webName.co.uk it will use urls.py from app B This is the urls.py from the main project: urlpatterns = [ url(r'^tinymce/', include('tinymce.urls')), url(r'^filer/', include('filer.urls')), url(r'^ckeditor/', include('ckeditor_uploader.urls')), url(r'^admin/', admin.site.urls), ] I was planning to add something like this to that file: if settings.CURRENT_HOST_IP == 'www.webname.co.id': urlpatterns += url(r'^', include('webname_id.urls')), else: urlpatterns

Passing objects from template to view using Django

守給你的承諾、 提交于 2019-12-06 15:49:55
I am trying to figure out the architecture for the following app: The user is presented with a table. Each table cell has several fields the user will be filling in. There is a general submit button: when clicked on all the input data (along with some calculated data per cell based on the input values) should pass to a Django view. Here are the following questions: Can I organize the data structure as a set of objects in a way that each object will correspond to a table cell, whereas the Master object, that will eventually be passed to the Django view, will be a set of those objects? If so,

How to find User queryset matching self.request.user

Deadly 提交于 2019-12-06 15:38:07
How do we find User queryset matching self.request.user? logged_in_user = User.objects.filter(id=self.request.user.id) I wish there is much efficient way to doing this. (such as get_user_model(self.request.user)) ? Here is my views.py class ProfilePageView(generics.RetrieveAPIView): serializer_class = ProfilePageSerializer def get_queryset(self): logged_in_user = User.objects.filter(username=self.request.user.username) << right here! return logged_in_user def get_object(self): queryset = self.get_queryset() obj = get_object_or_404(queryset) return obj There is no reason to get a queryset with

Django— Allowing Users to only edit their profile

[亡魂溺海] 提交于 2019-12-06 15:29:29
问题 I want to allow User to only edit their profile. This is my URL: url(r'^profile/(?P<pk>[0-9]+)/$', views.UserUpdate.as_view(), name='profile') Now when the user click on 'my profile' they will get their own profile which they can edit but if they manually edit the urlpath in browser and enter other user's id like below, they can view and edit other User's profile http://127.0.0.1:8000/profile/1/ this is my view class UserUpdate(UpdateView): model = Profile fields = ['personal_info','job_title

How to play a audio file through http response in django(python)

半腔热情 提交于 2019-12-06 15:14:50
问题 I want to make request to url and django view should read the file and send the http response back to play the same file in browser.I got the following code but it does't play anything please anyone help me.. Right now i hard coded the file name in the code. url: http://localhost/playfile/audiofile_name def playAudioFile(request): try: fname="C:\\test\\audio\\t.mp3" wrapper = FileWrapper(file(fname)) print content_type response = HttpResponse(wrapper, content_type="audio/mpeg") print response

How to link address model to views

◇◆丶佛笑我妖孽 提交于 2019-12-06 15:14:40
I'm trying to create an address form with multiple address, where the user can choose home or shipping address. I have the current model: from django.db import models from django.contrib.auth.models import User from PIL import Image class Address(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60, default="Miami") state = models.CharField(max_length=30, default="Florida") zipcode = models.CharField(max_length=5, default="33165") country = models.CharField(max_length=50) class Meta: verbose_name = 'Address'

How do I initiate values for fields in a form for editing in a template

谁说我不能喝 提交于 2019-12-06 15:08:27
I understand that you can use the initiate parameter for a Form class from this question . I am creating an edit form and I'm trying to figure out how to initiate values from a pre-existing object. Do I do it in the template level or in the view level (I don't even know how to do it in the template level)? Or maybe I need to pass the actual object to the form and initiate in the form level? What is the best practice? EDIT: For @Bento: In my original Form , I'm doing something like this class OrderDetailForm(forms.Form): work_type = forms.ChoiceField(choices=Order.WORK_TYPE_CHOICES) note =

django remove unicode from query result

烂漫一生 提交于 2019-12-06 15:03:02
问题 Django query gives me below output format,but i want below format data=`[{'total': 1744, 'name: u'x'}, {'total': 13, 'name': u'm'}, {'total': 126, 'role': name'n'}]` m=[] for i in data: m.append(i.values()) print m it give me output [[1744,u'x'], [13,u'm'], [126,u'n']] but i need output in how to remove unicode symbol from output [['x',1744], ['m',13], ['n',126]] how to do this ? Thanks in advance 回答1: Try this: >>> import json >>> data=[{'total': 1744, 'name': u'x'}, {'total': 13, 'name': u

django/taggit - unhashable type: 'list'

此生再无相见时 提交于 2019-12-06 15:02:37
问题 I'm using django-taggit (see here). This is what I have: forms.py from taggit.forms import * class MyForm(forms.Form): title = forms.CharField() my_tags = TagField(max_length=800, widget=forms.TextInput(attrs={'class':'myTags'})) views.py if 'submit_button' in request.POST: form = MyForm(request.POST) if form.is_valid(): cd = form.cleaned_data f_title = cd['title'] f_my_tags = cd['my_tags'] p = MyData.objects.create(title=f_title) p.tags.add(f_my_tags) p.save() mytemplate.html {{ form.my_tags

how to use iframe instead of div with jquery ui tabs?

孤街醉人 提交于 2019-12-06 14:31:16
问题 I have 4 tabs: <script type="text/javascript"> $(document).ready(function(){ $("#tabs").tabs({cache: true}); }); </script> <div id="tabs"> <ul style="padding: 0; margin: 0;"> <li class="context-tab" target="story-iframe"><a id="recent-tab" href="/canvas/getstories?context=recent">Recent</a></li> <li class="context-tab" target="story-iframe"><a id="popular-tab" href="/canvas/getstories?context=popularity" target="points-view">Popular</a></li> <li class="context-tab" target="story-iframe"><a id