django-views

One url pattern for two models in Django

99封情书 提交于 2019-12-02 09:49:50
问题 Is it possible to have one url pattern for two models in Django? I have two models: Game and Category and I want one url pattern for both of these: ios-games/category_name and ios-games/game_name So category pattern should go first and if slug is not there, it should check game pattern. Is it possible to do without creating one big view for both these models? Unfortunately, order of paths in url.py doesn't work, if it can't find object in the first pattern it won't go looking further... 回答1:

ValueError when getting objects by id

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 09:01:28
问题 I'm trying to get data by id in my django app. The problem is that I don't know the kind of id the user will click on. I input the below codes in views. Views def cribdetail(request, meekme_id): post=Meekme.objects.get(id=meekme_id) return render_to_response('postdetail.html',{'post':post, 'Meekme':Meekme},context_instance=RequestContext(request)) Urlconf url(r'^cribme/(?P<meekme_id>)\d+/$', 'meebapp.views.cribdetail', name='cribdetail'), In template: <a href="{% url cribdetail post.id %}">{{

Implementing http404 in django

∥☆過路亽.° 提交于 2019-12-02 08:33:47
Implementing page not found in django and have looked at the documentation 404 I do not get a page not found error as yet what ami doing here In my code in urls i have done the following, url(r'^$', 'site_config.views.pagenotfound') from django.http import Http404 def pagenotfound(request): return render_to_response('polls/pagenotfound.html', {}) The way you handle 404 and 500 in django is: by default, in the templates directory, create a 404.html If you need a custom handler, just add these to urls.py handler404 = 'views.page_not_found_custom' handler500 = 'views.page_error_found_custom'

Validating a form with overloaded _init_

橙三吉。 提交于 2019-12-02 08:25:30
I have a form with a new init method, which allow to display various choices according to a parameter : class Isochrone_Set_Parameters(forms.Form): Grid_Choices = Grids_Selection.Grid_Choices def __init__(self, Grid_Type, *args, **kwargs): super(Isochrone_Set_Parameters, self).__init__(*args, **kwargs) if Grid_Type == Grids_Selection.Grid_Values[0]: Choices = (('0.0','0.0'),('0.1','0.1'),('0.3','0.3'),('0.5','0.5'),('0.6','0.6'),('0.7','0.7'), \ ('0.8','0.8'),('0.9','0.9'),('0.95','0.95')) self.fields['Rotation_Rate'] = forms.ChoiceField(choices=Choices) elif Grid_Type == Grids_Selection.Grid

ajax with django forms

无人久伴 提交于 2019-12-02 07:53:43
can i add the ajax code with django? i have created a simple registraion form that have 5 fields . i wish to disply the each fields in different pages but in a single window . it means by using next button 5 pages want to disply in a single window. same time all content of each page i want add to my database. is this possible in django with ajax.. my codes are as follows : #view from django.shortcuts import render_to_response from registration.models import UserDetails from forms import UserForm from django import forms from django.template import RequestContext from django.http import

Django QuerySet returns nothing

一世执手 提交于 2019-12-02 07:29:54
I have a list of countries, they all have there own url www.example.com/al/ for example. There is a list of cities for every country but the object_list is empty My View: class CityOverview(generic.ListView): template_name = 'shisha/pages/country_index.html' model = City def get_queryset(self, *args, **kwargs): country_id = self.kwargs.get('country_id') return City.objects.filter(country__name=country_id) My Model: class Country(models.Model): COUNTRY_CHOICES = ( ('al', 'Albania'), ('ad', 'Andorra'), #etc. etc. ) name = models.CharField(max_length=255, choices=COUNTRY_CHOICES, default='nl')

Add users to groups in Django

元气小坏坏 提交于 2019-12-02 06:53:46
I am using the Group module in Django. I've created views GroupCreateView and GroupUpdateView in which I can update permissions and group name, but I also want to add users to the groups. Right now I have to update each user object and set to which groups it belongs. I want to do it the other way around where I create groups and add users to this group. How is this obtained? I guess it's something like group.user_set.add(user) I am assuming you want to add a newly created user to an existing group automatically . Correct me if I am wrong since this is not stated in your question. Here is what

Django: Using a variable as the URL namespace?

纵饮孤独 提交于 2019-12-02 06:22:12
I'm trying to create a submenu for a sports site. Each sport would need its own submenu. The problem I'm having is I need the namespace itself to be dynamic in someway. SportListView returns the sport so I can then filter the news articles by the sport. Views: class SportListView(ListView): template_name="sports/sport-home.html" context_object_name='sport_list' def get_context_data(self, **kwargs): context = super(SportListView, self).get_context_data(**kwargs) context['sport_menu'] = get_object_or_404(Sport, sport_slug=self.kwargs['sport_slug']) return context Template: <nav class="navbar

Django: UnboundLocalError: local variable 'company' referenced before assignment

谁说我不能喝 提交于 2019-12-02 05:19:00
I am trying to make a url field in my detail view by passing two primary key in it... This is what I have done in urls.py: url(r'^company/(?P<pk1>\d+)/groupdetail/(?P<pk2>\d+)/$',views.group1DetailView.as_view(),name='groupdetail'), And in my views: def get_object(self): pk1 = self.kwargs['pk1'] pk2 = self.kwargs['pk2'] company = get_object_or_404(company, pk=pk1) group1 = get_object_or_404(group1, pk=pk2) return group1 I am getting error in this line: company = get_object_or_404(company, pk=pk1) And in my group1 list view I have done this: <a href="{% url 'accounting_double_entry:groupdetail'

Choose queryset for limit_choices_to based on object fields

社会主义新天地 提交于 2019-12-02 04:10:46
I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't know if limit_choices_to is the right way to do this. Maybe I should choose the queryset in a or the