django-views

How To Use Django Cycle Tag

帅比萌擦擦* 提交于 2019-12-05 05:58:18
This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but don't know they mean by 'row1' and 'row2' : {% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> ... </tr> {% endfor %} Secondly, how would I correctly implement it into my template? I am using a very simple JS library , which will allow the sorting: page.html {% if Variable %} <table class="sortable"> <tr> <th>Title 1</th> <th>Title 2</th> <th>Title 3</th> <th>Title 4</th> <th>Title 5</th>

Django: change inlines based on select option

时光毁灭记忆、已成空白 提交于 2019-12-05 05:40:58
问题 Category has "types" (for example, three types of categories). Each category may have any number of Videos. And each Video, published in Category of '1' type may have any number of Pictures. But for Video, published in '2' and '3' Category types there are no Pictures. models.py : class Category(models.Model): title = models.CharField() CHOICES = ( ('1','1'), ('2','2'), ('3','3'), ) type = models.CharField(choices=CHOICES) class Video(models.Model): category = models.ForeignKey(Category) class

Django Get ImageField Path

别说谁变了你拦得住时间么 提交于 2019-12-05 05:30:39
models image_url = models.ImageField(upload_to="uploads/shows",blank=True,null=True) I am having image_url which I need to parse it using JSON object to my android application. I only need the URL of the image(absolute/relative). I have tried image_url=myObj.file.url image_url=myObj.image_url The correct working solution as provided by @alecxe is image_url =myObj.image_url.url 来源: https://stackoverflow.com/questions/18740651/django-get-imagefield-path

Django Management Form is failing because 'form-TOTAL_FORMS' and 'form-INITIAL_FORMS' aren't correctly populated

谁说我不能喝 提交于 2019-12-05 05:03:06
问题 Information: I would like to create nested forms as is best described through the example provided at: http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ The tutorial at this page seems to be pretty good && it is attempting to accomplish the exact problem I am encountering. There seems to be an issue with this implementation in the views.py file when there is no POST request data (I.e. we are performing the initial population from database). The code can be seen at the URL

Paginate Class-based View in Django

核能气质少年 提交于 2019-12-05 04:46:24
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(self): self.title = get_object_or_404(Title, slug=self.kwargs['title_slug']) return Issue.objects

Proper way to handle multiple Django forms in one page with two views?

 ̄綄美尐妖づ 提交于 2019-12-05 03:42:50
问题 I've struggled with this problem for the last two days and could use some help. The home page for my Django 1.6 application will include two forms, one that a user can use to sign in to the site and one they can use to sign up (create a login) for the site: # templates/home/home_page.html <div class="sign-in-form"> <form action="{% url 'apps.home.views.sign_in' %}" method="post"> {% csrf_token %} {{ sign_in_form.as_p }} {% if next %} <input type="hidden" name="next" value="{{ next }}"> {%

How to specify something other than pk or slug for DetailView

≯℡__Kan透↙ 提交于 2019-12-05 03:13:16
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? A slug doesn't have any particular significance in Django. It's just a name for a field that identifies a row. If your slug is called something else

Django best practice with foreign key queries

∥☆過路亽.° 提交于 2019-12-05 02:17:47
问题 models.py class Category(models.Model): name = models.CharField(max_length=50) class SubCatergory(models.Model): parent_category = models.ForeignKey(Category) name = models.CharField(max_length=100) views.py def all_products(request): c = Category.objects.all() s = SubCatergory.objects.all() return render_to_response('all_products.html', {'c':c, 's':s}) all_products.html {% for category in c %} <h1>{{ category.name }}</h1> <ul> {% for sub in s %} {% if category.id == sub.parent_category.id %}

How to Create Session Variables in Selenium/Django Unit Test?

谁说我不能喝 提交于 2019-12-05 01:36:33
问题 I'm trying to write a functional test that uses Selenium to test a Django view. When the user comes to a page ("page2"), the view that renders that page expects to find a session variable "uid" (user ID). I've read a half dozen articles on how this is supposed to be done but none of them have worked for me. The code below shows how the Django documentation says it should be done but it doesn't work for me either. When I run the test, the view never completes executing and I get a "server

Django dynamic Form example

被刻印的时光 ゝ 提交于 2019-12-05 01:33:51
I have a simple requirement for creating a dynamic form in Django - I've seen many examples but they seem to be incomplete, or require more extensive knowledge of Python and Django than I have! None show how the dynamic portion of the example should be called: This is the form class with Q1 and Q2 - I place a button on the form to add another field called Q3 - and then Q4 if pressed again: I think I got the init function semi correct: class testform(forms.Form): Q1 = forms.CharField() Q2 = forms.CharField() def __init__(self, *args, **kwargs): super(testform,self).__init__(*args,**kwargs) self