im working with django and making webapp now this has occurred
def Notes(generic.DetailView):
^
Error invalid syntax
Function name:NotesDetaiView
It should look like this. Check the documentation.
from django.views.generic.detail import DetailView
from .models import Notes # or other location
class NotesDetailView(DetailView):
model = Notes
# rest of code
Related
I'm new in django development. I m developing an API in which i am sending data from client side (mobile app) and this data is to be stored in a database using django. And if I query the data the data should be fetched from the DB. Database is preferably postgres/mysql DB. I have written some part of the code but stuck how to proceed. I'll appreciate if someone can guide me how to proceed.
from django.shortcuts import render
from rest_framework.views import APIView
from django.http import Http404
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import json
# Create your views here.
#api_view(["POST"])
def getIdealWeight(heightData):
try:
height=json.loads(heightData.body)
weight=str(height*10)
return JsonResponse("the ideal weight is:"+weight+" kg.",safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
Judging from the code, I'll advice you start from the very beginning of DRF Tutorials and cover a great part of it before going further with your project. After the quickstart, you should visit the individual parts dedicated to the major concepts like serializers, views, etc. Jumping straight to the project without having a fair knowledge of the framework will make it difficult to achieve what you want.
I'm trying to perform a simple insert operation using Mongoengine and Django.
Regarding my project structure simply I have a project, AProject and an app, AnApp. I have a running mongo in a remote machine with an IP of X.X.X.X. I am able to insert document using Robomongo in it.
I have removed the default Database configuration part of the settings.py located inside the AProject directory. The newly added lines are shown below:
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
import mongoengine
# ----------- MongoDB stuff
from mongoengine import register_connection
register_connection(alias='default', name='AProject', host='X.X.X.X')
# ----------
Now let me show the models.py and the views.py located inside AnApp.
models.py
from mongoengine import Document, StringField
class Confession(Document):
confession = StringField(required=True)
views.py
from django.http import HttpResponse
from models import Confession
from mongoengine import connect
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession()
confession.confession = 'First confession from the API'
print(confession.confession + ' Printable') # The output is --First confession from the API Printable--
print(confession.save()) # The output is --Confession object--
return HttpResponse(request)
The urls.py located inside AProject is simply as below:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^confessions/', include('confession.urls')),
url(r'^admin/', admin.site.urls),
]
When I enter http://127.0.0.1:10000/confessions/ I see a blank screen which I expect. However there is nothing saved from the API. I get the expected output except Confession object.
How can I solve this problem?
EDIT:
I found the concrete proof that currently MongoEngine's support for Django is unstable and it is corresponding to Django version 1.9:
MongoEngine documentation on Django support states:
Django support has been split from the main MongoEngine repository. The legacy Django extension may be found bundled with the 0.9 release of MongoEngine.
and
6.1. Help Wanted!
The MongoEngine team is looking for help contributing and maintaining a new Django extension for MongoEngine! If you have Django experience and would like to help contribute to the project, please get in touch on the mailing list or by simply contributing on GitHub.
Which leads to this repo, where the following is stated:
THIS IS UNSTABLE PROJECT, IF YOU WANT TO USE IT - FIX WHAT YOU NEED
Right now we're targeting to get things working on Django 1.9
So it may be possible that it cannot play well with Django at this state or with your version and thus the problem occurs.
Initial attempt, leaving it here for legacy reasons.
I believe that the problem occurs on how you are initializing your object, although I do not have a set up to test this theory.
It is generally considered that is better to make a new object with the .create() method:
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession.objects.create(
confession='First confession from the API'
)
print(confession.confession + ' Printable')
confession.save()
return HttpResponse(request)
Have a look at the Django & MongoDB tutorial for more details.
In this tutorial, the supported Django version is not mentioned, but I haven't found concrete proof that MongoDB Engine can or can't play well with Django version > 1.8.
Good luck :)
I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).
I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:
python manage.py runserver
I get the following error:
Since I am new to Django, I do not know what is going on. Please help.
from django.http import HttpResponse
in your views file at the top
Put this import in your poll/views.py before using HttpResponse.
from django.http import HttpResponse
from django.http import HttpResponse
add this line on the top of polls/views.py file. I am new too, and had the same error. good Luck and i see u around.
in your polls/views.py
By default is :
from django.shortcuts import render
change to:
from django.shortcuts import render,HttpResponse
this will call the HttpResponse class
In my case the import was there, but when I called HttpsResponse I called it with small h as a typo instead of the capital H
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello!") #==> This one was with httpResponse so the same error been received.
I had imported HttpResponse and still got this error.
If you use Apache server as your primary server for web, try restarting Apache and reloading the page.
For me it was because I used singe quotes (') instead of double quotes (")
Check your import statement.
Check your function. I had "HttpsResponse" instead of "HttpResponse"
Good luck.
I'm trying to create migrations of a project which uses Rest Framework pagination.
But I'm getting error as Attribution error: 'module' object has no attribute BasePaginationSerializer. I have tried uninstalling-resinstalling the said versions of Python, Django and RestFramework. But I still get that error.
Here's the screenshot of terminal showing error.
Here's a chunk of the code present in paginator.py
import urlparse
from django.core.paginator import EmptyPage, Page, PageNotAnInteger, Paginator
from django.utils.http import urlencode
from rest_framework import serializers, pagination
class CustomPaginationSerializer(pagination.BasePaginationSerializer):#Here it shows the error.
meta = MetaSerializer(source='*')
results_field = 'objects'
Can someone help me out in getting this issue resolved?
Info:
- Here's the project which I'm trying to build. https://github.com/mozilla/zamboni
I'm using Ubuntu 15.10
Python - 2.7.10
Django - 1.8.7
Django Rest Framework doesn't provide anything like BasePaginationSerializer, that's why you're getting an error - because it doesn't exist. You probably want to use BasePagination
Guys I am following "https://docs.djangoproject.com/en/1.5/intro/tutorial02/" tutorial and I successfully created models and displayed. But when I am trying to alter admin form its giving me error "name error: name 'admin' is not defined. I also need help from any of you guys to give me a simple but complete worked project as that will be easy for me to dig out problems. I am using Notepad ++, python 2.7 and Django 1.5. Using mysql workbench as db.
In models.py I have
class PollAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question']
def __unicode__(self):
return self.admin
In admin.py I got
from django.contrib import admin
from polls.models import Poll
admin.site.register(Poll, PollAdmin)
Thanking you in advance for your kind replies.
You should define PollAdmin in admin.py, not in models.py.
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question']
admin.site.register(Poll, PollAdmin)
I had this issue in my to-string method, I used the field without using the self.
def str(self):
return f'{self.first_name}-score {score}'
This will give you the name error, while
def str(self):
return f'{self.first_name}-score {self.score}'
will correct it for you.
I hope it works for you!