django-views

Django : Create custom object list in the view and pass it to template to loop over

北城以北 提交于 2019-12-11 04:24:51
问题 I want to create a custom object list in the view and pass it to the template. In the template I want to loop over the list and display the information. My models are class CustomUser(AbstractUser): def __str__(self): return self.email class Post(models.Model): author = models.ForeignKey(CustomUser,on_delete=models.CASCADE,) text = models.TextField() created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) post_url = models

Django saving many to many entries using generic views

点点圈 提交于 2019-12-11 04:18:49
问题 I have a django model with many to many fields as follows: class Sponsor(CommonInfo, Person): signature_code = models.CharField(max_length=20, blank=True) account = models.ForeignKey(Account) department = models.ForeignKey(Department) product = models.ManyToManyField(Product, null=True, blank=True, through = 'ProductSponsor') accounts = models.ManyToManyField(Account, null=True, blank=True, through = 'AccountSponsor', related_name='sponsoraccounts') and my views has: class SponsorEdit

Django authentication with custom user model with email-id as unique key

不问归期 提交于 2019-12-11 04:06:18
问题 to achieve unique constraint (without repetition of default user models fields) on email-id I did this from django.contrib.auth.models import AbstractUser class User(AbstractUser): # some extra fields class Meta: unique_together = ('email', ) and then in settings file AUTH_USER_MODEL = 'myApp.User' mysql table shows both column(username and email) as UNI key. so far looks good but when it comes to authentication I'm not sure where I'm making mistake. but its not working/loggin-in this code

showing the model values of specific user django

瘦欲@ 提交于 2019-12-11 03:43:24
问题 I want to show the data of a user which he has entered. This is my model class IgaiaContent(models.Model): CONTENT_CHANNELS = ( ('YouTube','Youtube'), ('FaceBook','FaceBook'), ('Flickr','Flickr'), ('Instagram','Instagram'), ) content_name = models.CharField(max_length=255, primary_key=True) content_type = models.CharField(max_length=255,null=True) content_source = models.CharField(max_length=255,null=True, choices=CONTENT_CHANNELS) content_location = models.CharField(max_length=255,null=True)

Django Many-To-Many Relationship (category)

廉价感情. 提交于 2019-12-11 03:38:36
问题 My goal is to add categories to my Post model. I would like to be able to later query all post by different and sometimes multiple categories. models.py class Category(models.Model): categories = ( ('1', 'red'), ('2', 'blue'), ('3', 'black') ) title = models.CharField(max_length=50, blank=True, choices=categories) class Post(models.Model): text = models.CharField(max_length=500) category = models.ManyToManyField(Category) forms.py class Post_form(forms.ModelForm): categories = ( ('1', 'red'),

Function-based generic views have been deprecated

前提是你 提交于 2019-12-11 03:07:37
问题 DeprecationWarning: Function-based generic views have been deprecated; use class-based views instead. I keep getting this warning, when I'm running my site. What does this mean and how can I fix it? 回答1: Django 1.3 introduced class-based views and also added a range of generic class-based views. Django also has documentation about migrating function-based views to class-based views. There is not really much more to say about it: your project currently uses function-based views whereas class

changing the field name django uses when display form error messages

久未见 提交于 2019-12-11 02:54:58
问题 Okay so I have a form and then in the template, I have this code: {% if form.errors %} { form.errors %} {% endif %} Now, suppose I have a username field in my form which is required and I purposely do not fill it out. It would say username -This field is required How do I change the word "username" which it displays when telling what field the error message is for? I'm guessing it is taking the name from the Model I created because in the model (which I then made a form of) I had written

Django @property used in .aggregate()

二次信任 提交于 2019-12-11 02:53:51
问题 I'm trying to aggregate a list of integers from a model. The field that the integers are derived from are an @property decorator field. The decorator works as expected and within the template.html , if passed directly, displays without a problem. If however, I try and pass the @property field through .aggregate() the context passed into the template throws an error that basically says Cannot resolve keyword 'sum_thing' into field. followed by a list of model fields that don't include any of

GET request with request parameter in Django Rest Framework

风格不统一 提交于 2019-12-11 02:28:11
问题 I am trying to do GET with a request parameter in Django Rest. views.py :- class ItemView(generics.ListCreateAPIView): queryset = itemlist.objects.all() serializer_class = ItemListSerializer def perform_create(self, serializer): serializer.save() def get_queryset(self): queryset = itemlist.objects.all() get_param = self.request.GET.get('get_param') if get_param: queryset = queryset.filter(item_name=get_param) return queryset urls.py :- urlpatterns = { url(r'^itemlists/$', ItemView.as_view(),

How to add extra data to a Django model for display in template?

自古美人都是妖i 提交于 2019-12-11 01:58:25
问题 My Django Model: class myModel(models.Model): myIntA = models.IntegerField(default=0) My View: myModelList = myModel.objects.all() for i in range(len(myModelList)): myModelList[i].myIntB = i return render( request, 'myApp/myTemplate.html', Context( { "myModels": myModelList, } ) ) Is the above legal? You can see that I added a variable myIntB to each myModel object. However when I try to print myIntB in the template below, nothing shows up. How can I access myIntB from the template? It is not