django-views

Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'

帅比萌擦擦* 提交于 2019-11-28 08:01:24
I'm new to Django and am trying to create the back end code for a music application on my website. I have created the correct view in my views.py file (in the correct directory) as shown below: def detail(request, album_id): return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>") however, when creating the url or path for this (shown below) #/music/71/ (pk) path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'), I am experiencing a warning on my terminal stating: ?: (2_0.W001) Your URL pattern '^(?P<album_id>[0-9])/$' [name='detail'] has a route that contains '(?P<',

Unable to import path from django.urls

百般思念 提交于 2019-11-28 08:01:19
Tried to run command: from django.urls import path Getting error: Traceback (most recent call last): File "< stdin >", line 1, in ImportError: cannot import name 'path' I am using django version 1.11 The reason you cannot import path is because it is new in Django 2.0 as is mentioned here: https://docs.djangoproject.com/en/2.0/ref/urls/#path . On that page in the bottom right hand corner you can change the documentation version to the version that you have installed. If you do this you will see that there is no entry for path on the 1.11 docs. You need Django version 2 pip install --upgrade

How to organize and load CSS within Django project/app?

瘦欲@ 提交于 2019-11-28 07:53:42
I'm new to Django. I read the Documentation and I am literally confused with media root files and static root files and where I should be placing my css files along with my javascript files. Can you guys point me in the correct direction? Project setup: Project -myapp -model,view,and test -template - base.html - index.html - view1 - view2 - media - css - style.css - ie.css - js - img -setting.py -url.py What should my static_url, static_root, media_url , media_root, STATICFILES_DIRS and STATICFILES_FINDERS look like? How I currently load my css file is through the base.html with the following:

Django 1.5 - using the new StreamingHttpResponse

北慕城南 提交于 2019-11-28 07:42:09
问题 If I implement StreamingHttpResponse as shown here, the 'streaming' response is not shown until the 10 seconds is up. There isn't much information on djangoproject except saying it's useful for generating large CSV files while warning that expensive tasks should be performed outside of the request-response cycle. However, I cannot see that it is working at all using time-intensive code. Is there something about the generator object that prevents this? Here is the code duplicated for reference

ValueError: Missing staticfiles manifest entry for 'favicon.ico'

非 Y 不嫁゛ 提交于 2019-11-28 06:47:43
I'm getting a ValueError when running python manage.py test . My project is named fellow_go , and I'm currently working on an App called pickup . Please note that this error is added in a relatively recent commit to Django: Fixed #24452 -- Fixed HashedFilesMixin correctness with nested paths. . ====================================================================== ERROR: test_view_url_exists_at_desired_location (pickup.tests.test_view.HomePageViewTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/sunqingyao

How to extend UserCreationForm with fields from UserProfile

走远了吗. 提交于 2019-11-28 06:12:13
问题 I found this post on how to extend the UserCreationForm with extra fields such as "email." However, the email field is already defined in the pre-built user model. I created an extra model (called UserProfile) that futher extends Django's pre-built User class. How do I get these fields I defined in UserProfile to appear in my UserCreationForm? 回答1: Add fields as appropriate for your UserProfile model (it's not too easy to use a ModelForm to avoid Repeating Yourself, unfortunately), then

Set initial value to modelform in class based generic views

人盡茶涼 提交于 2019-11-28 05:51:51
I'm using Class based generic views, can anybody suggest me how can i set the initial values to update form? I tried using get_initial() method but didn't got any success. Following is the code which i tried class IncidentUpdateView(UpdateView): form_class = IncidentForm form_class.initial = {"badge_number": '88888'} model = Incident template_name = 'hse/incident/incident_update.html' def get_initial(self, form_class): initials = { "badge_number": '88888' } form = form_class(initial=initials) return form def get_success_url(self): return reverse_lazy('hse-incident', args=[self.object.id]) You

How can I pass data to any template from any view in Django?

筅森魡賤 提交于 2019-11-28 05:40:39
问题 Like a good little coder, all of my Django templates inherit from a base.html. Now I would like to add some functionality to the base to always show some interesting things. Some user statistics, or random posts, or feeds, etc. All of my views look like this: def viewname(request) : template_vales = {} // Stuff return render_to_response('some_file_name.html', template_values) How can I make it so that the values of template_values are always populated for all my views? Do I have to do this at

Django - ModelForm Create or Update? [duplicate]

百般思念 提交于 2019-11-28 05:33:15
This question already has an answer here: How to update an object from edit form in Django? 2 answers I need to have a form that allows the creation or addition of sessions on a planning Model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() Form class SessionForm(forms.ModelForm): class Meta: model = Session exclude = ['tutor'] View to render the form def editor(request): if request.method == 'GET': if request.GET['id'] != '0': # The user has selected a session session = Session.objects.get(id=request.GET['id'])

Can I have a Django form without Model

…衆ロ難τιáo~ 提交于 2019-11-28 05:18:24
Can I have a Form in my template which is not backed by a model. I do not need to store the data just need that data to generate a POST request of my own in the view. Template - The form with text fields. View - get data from form, and generate another request. Flow --> Form submit takes to a url which calls the view " def form_handle(request): if request.method=='POST' form = request.POST #blah blah encode parameters for a url blah blah #and make another post request but this puts only the csrf token into the form variable. Is there some way I can access those text fields of the template in