django-views

Django - Error models with control data

喜你入骨 提交于 2019-12-07 13:30:43
问题 I'm working on writing the models.py file, and I need some controls to perform the automatic compilation of some fields. MOTIVOINGRESSO = ( (u'sequestro ', u'sequestro'), (u'fermo ', u'fermo'), (u'confisca final ', u'confisca final'), (u'cambio custodian ', u'cambio custodian'), ) motivo_ingresso = models.CharField (max_length = 50, choices = MOTIVOINGRESSO) FERMO = ( (u'30 ', u'30'), (u'60 ', u'60'), (u'90 ', u'90'), (u'180 ', u'180'), (u'1 month ', u'1 month'), (u'3 months, 'u'3 months'),

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

筅森魡賤 提交于 2019-12-07 07:49:27
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, 'model': 'DocumentGroup' 'field': { 'text':'DocumentGroup1' } } ] } }, { 'pk': 2, 'model': 'DocumentClass'

What's the difference between the two methods of decorating class-based views?

房东的猫 提交于 2019-12-07 06:44:41
问题 I'm writing a view that inherits from ListView, and am trying to restrict the view to logged-in users. https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-in-urlconf says that decorating with login_required in the URLconf "applies the decorator on a per-instance basis. If you want every instance of a view to be decorated, you need to take a different approach" -that approach being to decorate the dispatch method in the view code. I thought I knew the difference between

Django - Add rows to MySQL database

对着背影说爱祢 提交于 2019-12-07 06:20:01
问题 So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in. I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now.. 回答1: Of coures this is possible this is a

How to redirect to a different view after processing a form getting NoReverseMatch error

不羁的心 提交于 2019-12-07 06:07:21
问题 I am making a blog app and I want to redirect to a different different url after I have processed the form, however the below given view is not working. I am unable to use neither HttpResponseRedirect nor simply redirect @login_required def blog_form(request,author_id=None,slug=None): context_instance=RequestContext(request) # This view will have a valid creator_id and slug field if the # blog is being edited and in this case the creator and user should be same if ( author_id and slug):

Calling Django View from Ajax

旧巷老猫 提交于 2019-12-07 05:23:51
问题 I'm using Ajax (along with Django) to perform some action on button click. I successfully call the javascript function but I can't call the Django view. There are no errors but the print statement in my view doesn't print...? urls.py urlpatterns = patterns('polls.views', url(r'^request_access/$', 'request_access', name='request_access'), ) views.py def request_access(request): print("DJANGO VIEW") if request.method == "POST": print("DATA: ", request.POST.get('request_data')) return

django pdf export

末鹿安然 提交于 2019-12-07 05:16:42
问题 I want to generate a PDF which will show the output of my queryset in table format, for example: query = ModelA.objects.filter(p_id=100) class ModelA(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=200) p_id = models.IntegerField() description = models.TextField() I need to show the values for name , description and pid in the generated PDF. 回答1: As mentioned by other people the best way to do this is to generate a template then convert the result,

Django : Call a method only once when the django starts up

杀马特。学长 韩版系。学妹 提交于 2019-12-07 05:04:22
问题 I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be only called once. Tried looking in other Pages, but couldn't find an answer to it. The code currently looks something like this :: def get_latest_dbx(request, ....): #get the data from database def get_latest_x(request): get_latest_dbx(request,x,...) def startup(request): get_latest_x(request)

Newbie Django Model Error

橙三吉。 提交于 2019-12-07 04:56:21
问题 With Python 2.7.x + Django 1.9: I create a new super-simple Django skeleton project with django-admin startproject simple As a sanity check, I create a views.py file with a simple view that outputs a "hello world" type test message and a url route to that view. I can run this with python manage.py runserver and it works fine. I create a models.py file with a single super simple Django ORM model class. FYI, my goal is to use existing tables and schema, so I don't want the ORM to generate new

AttributeError: '…' object has no attribute '***_set'

◇◆丶佛笑我妖孽 提交于 2019-12-07 03:19:28
问题 I have class Question(models.Model): created_by = models.ForeignKey(User) question = models.CharField(max_length=150) def __unicode__(self): return self.question class Answer(models.Model): rel = models.ForeignKey(Question) answer = models.CharField(max_length=150) def __unicode__(self): return self.answer and if i do answer1 = "xyz" tmp = Question.objects.get(pk=1) tmp.answer_set.create(answer=answer1) i get AttributeError: 'Question' object has no attribute 'answer_set' What is wrong? EDIT: