django-views

What's the best way to serialize more than one model to json in Django 1.6?

穿精又带淫゛_ 提交于 2019-12-08 06:36:07
问题 I have 3 models show as below. class DocumentClass(models.Model): text = models.CharField(max_length=100) class DocumentGroup(models.Model): text = models.CharField(max_length=100) documentclass = models.ForeignKey(DocumentClass) class DocumentType(models.Model): text = models.CharField(max_length=100) documentgroup = models.ForeignKey(DocumentGroup) And my goal is something like this: [ { 'pk': 1, 'model': 'DocumentClass', 'fields':{ 'text':'DocumentClass1', 'documentgroup': [ { 'pk': 1,

Django - ImageField files not showing up?

狂风中的少年 提交于 2019-12-08 06:08:53
问题 This is my models.py: class UserImages(models.Model): user = models.ForeignKey(User) photo = models.ImageField(upload_to=get_file_path) and this is my view which uploads images: def uploadImageView(request): if request.method == 'POST': form = UploadImageForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.save() return redirect('/') else: form = UploadImageForm() return render(request, 'image.html', {'form': form}) and

How to make infinite threaded comments

空扰寡人 提交于 2019-12-08 05:31:49
问题 My current code allows me to render a queryset of Comments (parent comments) as well as replies to those comments. But i'm unable to render replies to those replies. My goal is to have an infinite reply system. Here's my code: class Comment(models.Model): user = models.ForeignKey(User, blank=True, null=True) destination = models.CharField(default='1', max_length=12, blank=True) parent_id = models.IntegerField(default=0) parent_comment = models.ForeignKey('self', related_name='replies',

How to link address model to views

ぃ、小莉子 提交于 2019-12-08 05:04:26
问题 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

Django - using reverse() on Class-based views

天涯浪子 提交于 2019-12-08 04:44:35
问题 I have the following urls configuration in my Django project: urlpatterns = patterns('', (r'^my-view$', MyViewClass.as_view()), ) Is there a way to use the reverse() function to get the url of the above view? 回答1: Yes there is. Use the name argument of the url function to define a name for the url, then you can use reverse on this name: from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^my-view$', MyViewClass.as_view(), name='my_view'), ) reverse('my_view') 来源: https

How to find User queryset matching self.request.user

旧城冷巷雨未停 提交于 2019-12-08 04:02:32
问题 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

Django Parse Post Request data (JsonArray)

[亡魂溺海] 提交于 2019-12-08 03:19:26
I have a model Example with 3 fields. class Example(models.Model): name = models.CharField(max_length=200, null=False, blank=False) number = models.CharField(max_length=200, null=False, blank=False) address = models.CharField(max_length=200)` I have a Post API (rest framework). This would have array of objects. I want to parse this array and store each object in my database. This is the Views.py class PostExample(APIView): def post(self, request, format=None): example_records = request.POST["example_data"] print example_records Here "example_data" is the key and value will be an array. Example

Django: How to include a view from within a template

主宰稳场 提交于 2019-12-08 02:13:39
问题 I'm new to django and I was wondering what is the best/recommend approach to include a view (with certain logic, and its resulting HTML) from within a template. My concrete example is as follows: I have a model which is: class City(models.Model): name = models.CharField(max_length=200) class Place(models.Model): city = models.ForeignKey(City) name = models.CharField(max_length=200) state = models.IntegerField() So I need a view to show each city and it's places. Each place should be rendered

How to start forming a django website and how django structures pages?

早过忘川 提交于 2019-12-08 02:09:31
问题 I started a django project for my personal website to learn django. So far I've got my development environment set with everything I need and followed this great tutorial to create some basic data structures and templates. Now I would like to start using my html layout I made before and start implementing the functionalities to it. However I'm having hard time understanding how to accomplish this. I've mostly done java portal solutions before this where I could start the server, create some

Django Using Slug Field for Detail URL

99封情书 提交于 2019-12-07 22:30:32
I'm attempting to setup my site so that the url for my job-detail will use a slug field instead of a pk. It's telling me that it cannot find my job with the given slug (which is an int, 147). Update: After looking at the DetailView description at https://ccbv.co.uk/projects/Django/1.11/django.views.generic.detail/DetailView/ I realized there is a slug_field attribute for DetailView . My new view looks like: class JobDetailView(CacheMixin, DetailView): model = Job slug_field = 'slug' Question: urls: urlpatterns = [ url(r'^careers$', views.job_list, name='job-list'), url(r'^careers/(?P<slug>[0-9