django-views

File upload with Django: Invalid form

こ雲淡風輕ζ 提交于 2019-12-22 09:50:43
问题 First time posting a question. I've basically copied everything I've found on here and on the django documentation site and I can't figure out what I'm doing incorrectly Here is the code from my views.py file from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() def upload_file(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): response = "Form is valid." else: response = "Failed to upload

Calling a special (non-HTTP) URL from the form_valid method of a Django class-based view

为君一笑 提交于 2019-12-22 08:42:30
问题 There's this HTML trick where if you do <a href="sms:14085551212?body=Hello my friend">New SMS Message</a> , clicking New SMS Message opens up the phone's native SMS app and pre-fills the To field with the number provided (1-408-555-1212 in this case), and the body with the message provided ( Hello my friend in this case). Is there any way I can call this same href string from the form_valid method of a Django class-based view? To be exact, in this form_valid method I'm receiving a POST

Django : ModelForm with conditions

╄→гoц情女王★ 提交于 2019-12-22 08:36:23
问题 I'm trying to create a form variable. As default Player have the level 0 and he can just change is name. Later when he is level 1, he can change is name and is avatar. When he is level 3, he can change is name, is avatar and is job. Etc... Models.py: class Player(models.Model): level = models.SmallIntegerField(default=0) name = models.CharField(max_length=50) avatar = models.URLField(default='http://default-picture.com/01.png') job = models.TextField(null=True) Fomrs.py: class ProfileForm

How to cater for 'AnonymousUser' when filtering based on user

浪尽此生 提交于 2019-12-22 08:16:14
问题 I have the following model class Team(models.Model): name = models.CharField(max_length=100) members = models.ManyToManyField(User, related_name="members", blank=True, null=True) And the following view (controller) def my_teams(request): my_list = Team.objects.filter(members=request.user).order_by('name') return render_to_response('teams/index.html', {'my_list': my_list}) This works fantastic when a user is logged in, but I receive the following error while testing as an Anonymous user.

Paginate Class-based View in Django

孤者浪人 提交于 2019-12-22 04:42:48
问题 I am trying to paginate my class-based view. Here is how my view looks: class IssuesByTitleView(ListView): context_object_name = "issue_list" def issues(request): issue_list = Issue.objects.all() ###### Commented out does not work ###### # paginator = Paginator(issue_list, 24) # try: # page = int(request.GET.get('page', '1')) # except ValueError: # page = 1 # try: # issues = paginator.page(page) # except (EmptyPage, InvalidPage): # issues = paginator.page(paginator.num_pages) def get_queryset

How to specify something other than pk or slug for DetailView

血红的双手。 提交于 2019-12-22 04:20:51
问题 I was wondering if it was possible to use something besides a pk or slug when you are using a DetailView in Django 1.3. For example, I currently have: url(r'^mymodel/(?P<pk>\d+)/$', MyDetailView.as_view()) as my url. Say I wanted something like: url(r'^mymodel/(?P<name>\d+)/$', MyDetailView.as_view()) where name would be a field in the model. Is there anyway to have the DetailView use that to 'grab' the object I want and pass it on to my template? 回答1: A slug doesn't have any particular

How to specify something other than pk or slug for DetailView

我与影子孤独终老i 提交于 2019-12-22 04:20:05
问题 I was wondering if it was possible to use something besides a pk or slug when you are using a DetailView in Django 1.3. For example, I currently have: url(r'^mymodel/(?P<pk>\d+)/$', MyDetailView.as_view()) as my url. Say I wanted something like: url(r'^mymodel/(?P<name>\d+)/$', MyDetailView.as_view()) where name would be a field in the model. Is there anyway to have the DetailView use that to 'grab' the object I want and pass it on to my template? 回答1: A slug doesn't have any particular

Django calling REST API from models or views? [closed]

大憨熊 提交于 2019-12-22 03:50:33
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I have to call external REST APIs from Django. The external data source schemas resemble my Django models. I'm supposed to keep the remote data and local ones in sync (maybe not relevant for the question) Questions: What is the most logical place from where to call external web

django: return string from view

…衆ロ難τιáo~ 提交于 2019-12-22 01:53:50
问题 I know this is a simple question, sorry. I just want to return a simple string, no templates. I have my view: def myview(request): return "return this string" I don't remember the command. Thanks 回答1: According to the documentation: A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. Each view function is responsible for returning an HttpResponse object. In other words, your view should return a HttpResponse instance: from

Using fullCalendar in Django

六月ゝ 毕业季﹏ 提交于 2019-12-22 00:59:35
问题 Somewhat of a follow-up to the question posted here (Django: modifying data with user input through custom template tag?), but since asking the question I've decided to take a different approach. As you can tell I'm a newb so please go easy. I want a weekly calendar in my Django app that displays shifts in the database. Following is the Shift model. class Shift(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='shifts', blank=True, null=True) # A shift may not